poj 1703 Find them, Catch them

poj 1703 Find them, Catch them,第1张

poj 1703 Find them, Catch them
#include <stdio.h>#define MAXV 100010int ans[MAXV],tree[MAXV];int find(int x){    int rt;    if(tree[x]!=x){        rt=find(tree[x]);        ans[x]=ans[x]^ans[tree[x]];//类别偏移        return tree[x]=rt;    }    return x;}void TUnion(int x,int y){    int fx,fy;    fx=find(x);    fy=find(y);    tree[fx]=fy;    ans[fx]=~(ans[y]^ans[x]);//类别偏移}int main(){    int i,n,m,a,b;    char c;    int t;    scanf("%d",&t);    while(t--){        scanf("%d%dn",&n,&m);        for(i=0;i<=n;i++){ ans[i]=0; tree[i]=i;        }        for(i=1;i<=m;i++){ scanf("%c %d %dn",&c,&a,&b); if(c=='D'){     TUnion(a,b); }else{     if(n==2)  printf("In different gangs.n");     else if(find(a)==find(b)){  if(ans[a]==ans[b])      printf("In the same gang.n");  else      printf("In different gangs.n");         }else  printf("Not sure yet.n"); }        }    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4918867.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存