栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1703 Find them, Catch them

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375281.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号