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;
    
    }
    

  • 相关阅读:
    c++ map 的基本操作
    hdu Dragon Balls
    hdu Code Lock
    小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
    将.py脚本打包成.exe
    关于IO同步/异步/阻塞/非阻塞文章
    C++文件操作
    (十)参考教程
    (八)树控件(Tree Control),标签控件(tab control)
    (七)对话框,单选框(radiobox),复选框(checkbox),列表框(ListBox),组合框(CComboBox),水平滚动条(Horizontal scroll bar),微调(旋转)spincontrol,列表视图控件CListCtrl,静态控件static
  • 原文地址:https://www.cnblogs.com/acerkoo/p/9490320.html
Copyright © 2011-2022 走看看