poj 1703(并查集对不同集合的统计)

#include<iostream>
#include<cstring>
using namespace std;
#define maxn 100005
int par[2*maxn],height[2*maxn];
int Find(int x){
    if(x==par[x])return x;
    else return Find(par[x]);
}
void Differ(int p,int q){
    int parP = Find(p),parQ = Find(q);
    if(height[parP]>height[parQ]){
        par[parQ] = parP;
        height[parQ]++;
    }
    else{
        par[parP] = parQ;
        if(height[parP]==height[parQ]){
            height[parP]++;
        }
    }
}
int main(){
    char ch[2];
    int t,n,m,p,q;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n*2;i++){
            par[i] = i;
        }
        memset(height,0,sizeof height);
        for(int i=0;i<m;i++){
            scanf("%s%d%d",ch,&p,&q);
            if(!strcmp(ch,"D")){
                Differ(p,q+n);
                Differ(p+n,q);
            }
            else{
                if(Find(p)==Find(q)){
                    printf("In the same gang.\n");
                }
                else if(Find(p)==Find(q+n)||Find(p+n)==Find(q)){
                    printf("In different gangs.\n");
                }
                else
                    printf("Not sure yet.\n");
            }
        }
    }
    return 0;
} 

 

上一篇:No module named ssl


下一篇:函数的使用