poj1703find them,catch them

来源:互联网 发布:辐射3 mac 编辑:程序博客网 时间:2024/05/31 00:39

#include<stdio.h>#include<iostream>using namespace std;long set[505000];long rank[505000];long findset(long x){    long y=x;    if(set[x]==x) return x;    y=findset(set[x]);    rank[x]=(rank[set[x]]+rank[x])%2;    set[x]=y;    return y;}void unions(long x,long y){    long a,b;    a=findset(x);    b=findset(y);    set[b]=a;    rank[b]=(rank[x]+rank[y]+1)%2;}int main(){    long t,n,m,x,y,a,b;    char c;    scanf("%d",&t);    for(long i=0;i<t;i++){        //scanf("%d %d\n",&n,&m);        scanf("%d %d",&n,&m);        for(long j=1;j<=n;j++){            set[j]=j;            rank[j]=0;        }        for(long j=0;j<m;j++){            //scanf("%c %d %d\n",&c,&x,&y);            getchar();            scanf("%c %d %d",&c,&x,&y);            a=findset(x);            b=findset(y);            //cout<<a<<"   "<<b<<"   "<<rank[x]<<"   "<<rank[y]<<endl;            if(c=='D') unions(x,y);            else if(c=='A'){                if(a!=b) {cout<<"Not sure yet."<<endl; continue;}                if(a==b){                    if(rank[x]==rank[y]) {cout<<"In the same gang."<<endl; continue;}                    else {cout<<"In different gangs."<<endl; continue;}                }            }        }    }    return 0;}

好吧。。。我只是为了刷并查集,看题的时候莫名的觉得这题很熟悉。。。结果居然是做过的。。。难道我刷错了,blog里又米题解诶。。。