其他分享
首页 > 其他分享> > poj 2492 A Bug's Life(种类并查集)

poj 2492 A Bug's Life(种类并查集)

作者:互联网

  1. #include<cstdio>int s[4020];//开两倍空间数组
    int find(int x){
    return x==s[x]?s[x]:s[x]=find(s[x]);//压缩路径,否则可能会超时
    }
    void Union(int x,int y){
    s[find(x)]=find(y);
    }
    int main(){
    int cnt=1,t;
    int m,n;
    scanf("%d",&t);
    while(t--){
    int flag=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<=2*n;i++)s[i]=i;
    while(m--){
    int a,b;
    scanf("%d%d",&a,&b);
    if(find(a)==find(b))flag=1;
    else{
    Union(a,b+n);
    Union(a+n,b);
    }
    }
    if(flag)printf("Scenario #%d:\nSuspicious bugs found!\n\n",cnt++);
    else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",cnt++);
    }
    }
     
       
    #include<cstdio>int s[4020];//开两倍空间数组
    int find(int x){
    return x==s[x]?s[x]:s[x]=find(s[x]);//压缩路径,否则可能会超时
    }
    void Union(int x,int y){
    s[find(x)]=find(y);
    }
    int main(){
    int cnt=1,t;
    int m,n;
    scanf("%d",&t);
    while(t--){
    int flag=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<=2*n;i++)s[i]=i;
    while(m--){
    int a,b;
    scanf("%d%d",&a,&b);
    if(find(a)==find(b))flag=1;
    else{
    Union(a,b+n);
    Union(a+n,b);
    }
    }
    if(flag)printf("Scenario #%d:\nSuspicious bugs found!\n\n",cnt++);
    else printf("Scenario #%d:\nNo suspicious bugs found!\n\n",cnt++);
    }
    }

标签:Life,查集,int,scanf,2492,cnt,Union,flag,find
来源: https://www.cnblogs.com/fanyu1/p/12403980.html