其他分享
首页 > 其他分享> > poj 1703(并查集对不同集合的统计)

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

 

标签:par,1703,int,查集,height,poj,parQ,parP,Find
来源: https://www.cnblogs.com/stevenzrx/p/15674012.html