其他分享
首页 > 其他分享> > P1892团伙 -- 并查集datastructure

P1892团伙 -- 并查集datastructure

作者:互联网

#include <bits/stdc++.h>

using i64 = long long;
int fa[1000005],b[1000005];
int find(int x){
    return (fa[x] == x ? x : (fa[x] = find(fa[x])));
}

void merge(int x,int y){
    fa[find(x)] = find(y);
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n,m;
    std::cin >> n >> m;
    for(int i = 1; i <= 20005; i++) fa[i] = i;
    while(m--){
        char c;int p,q;
        std::cin >> c >> p >> q;
        if(c == 'F'){
            merge(p, q);//如果是朋友就直接放在一起
        }else{
            merge(p + n, q);//敌人的敌人就是自己的朋友
            merge(q + n, p);//这种关系是互相的
        }
    }    
    int ans = 0;
    for(int i = 1; i <= n; i++){
        ans += fa[i] == i;
    }
    std::cout << ans;
    return 0;
}

 

标签:std,1000005,fa,--,查集,int,datastructure,merge,find
来源: https://www.cnblogs.com/zrzsblog/p/16479195.html