zoukankan      html  css  js  c++  java
  • 6492: Connectivity

    6492: Connectivity

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 128  解决: 34
    [提交][状态][讨论版][命题人:admin]

    题目描述

    There are N cities. There are also K roads and L railways, extending between the cities. The i-th road bidirectionally connects the pi-th and qi-th cities, and the i-th railway bidirectionally connects the ri-th and si-th cities. No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.
    We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads. We will also define connectivity by railways similarly.
    For each city, find the number of the cities connected to that city by both roads and railways.

    Constraints
    2≤N≤2*105
    1≤K,L≤105
    1≤pi,qi,ri,si≤N
    pi<qi
    ri<si
    When i≠j, (pi,qi)≠(pj,qj)
    When i≠j, (ri,si)≠(rj,sj)

    输入

    The input is given from Standard Input in the following format:
    N K L
    p1 q1
    :
    pK qK
    r1 s1
    :
    rL sL

    输出

    Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

    样例输入

    4 3 1
    1 2
    2 3
    3 4
    2 3
    

    样例输出

    1 2 2 1
    

    提示

    All the four cities are connected to each other by roads.
    By railways, only the second and third cities are connected. Thus, the answers for the cities are 1,2,2 and 1, respectively.

    来源

    ABC049&ARC065 

    题意:

    有公路和铁路两种路,走公路只能走公路,走铁路只能走铁路,求每个城市既能通过公路到达,又能通过铁路到达城市的个数,(城市既可以通过公路到达,又可以通过铁路到达自己)。

    思路

    并查集,求一下每个城市在公路并查集中的编号,铁路并查集中的编号,然后统计同属于这两个编号的城市个数。


    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+10;
    int fa[maxn],ma[maxn];
    void init(){
        for (int i=0;i<maxn;i++) fa[i]=i,ma[i]=i;
    }
    int fnd(int *k,int x){
        return x==k[x]?x:k[x]=fnd(k,k[x]);
    }
    void Union(int *k,int x,int y){
        int fx=fnd(k,x),fy=fnd(k,y);
        if(fx!=fy) k[fy]=fx;
    }
    map<pair<int,int>,int>mp;
    int main(){
        int n,k,l,u,v;
        init();
        scanf("%d%d%d",&n,&k,&l);
        while(k--){
            scanf("%d%d",&u,&v);
            Union(fa,u,v);
        }
        while(l--){
            scanf("%d%d",&u,&v);
            Union(ma,u,v);
        }
        for (int i=1;i<=n ; i++)  mp[make_pair(fnd(fa,i),fnd(ma,i))]++;
        for (int i=1;i<n;i++) printf("%d ",mp[make_pair(fnd(fa,i),fnd(ma,i))]);
        printf("%d
    ",mp[make_pair(fnd(fa,n),fnd(ma,n))]);
        return 0;
    
    }
    

  • 相关阅读:
    HTML&&CSS
    web概述&HTML快速入门
    JDBC连接池&JDBCTemplate
    基于Breast Cancer dataset的决策树分类及可视化
    三维数组按行优先存储求某位置的地址
    2019年复旦计算机专硕考研经验总结
    1013 Battle Over Cities (25 分)
    1009 Product of Polynomials (25 分)
    1004 Counting Leaves (30 分)
    1090 危险品装箱 (25 分)
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490320.html
Copyright © 2011-2022 走看看