1 solutions

  • 0
    @ 2022-4-8 11:59:10
    #include <bits/stdc++.h>
    using namespace std;
    
    int n,m,p,q;
    int fa[2][10005];//0 man 1 woman
    
    void init(){
        for(int i = 1 ; i <= n ; i++){
            fa[0][i] = i;
        }
        for(int i = 1 ; i <= m ; i++){
            fa[1][i] = i;
        }
    }
    int find(int flag,int a){
        while(fa[flag][a] != a) a = fa[flag][a];
        return a;
    }
    void merge(int flag,int a,int b){
        a = find(flag,a);
        b = find(flag,b);
        if(a != b){
            fa[flag][a] = b;
        }
    }
    
    int main(){
        int a,b;
        int man = 0,woman = 0;
        cin>>n>>m>>p>>q;
        init();
        for(int i = 1 ; i <= p ; i++){
            cin>>a>>b;
            if(a == b){
                continue;
            }
            merge(0,a,b);
        }
        for(int i = 1 ; i <= q ; i++){
            cin>>a>>b;
            a = 0 - a;
            b = 0 - b;
            if(a == b){
                continue;
            }
            merge(1,a,b);
        }
        for(int i = 1 ; i <= n ; i++){
            if(find(0,i) == find(0,1)){
                man++;
            }
        }
        for(int i = 1 ; i <= m ; i++){
            if(find(1,i) == find(1,1)){
                woman++;
            }
        }
        cout<<min(woman,man)<<endl;
        return 0;
    }
    

    Information

    ID
    1268
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    7
    Tags
    # Submissions
    74
    Accepted
    19
    Uploaded By