首页 > 其他 > 详细

poj 2492 A Bug's Life

时间:2018-09-13 23:55:16      阅读:257      评论:0      收藏:0      [点我收藏+]

带权并查集模2系,也就是给一些关系 ,这些关系是是否同类。问有多少关系错的。

 

 

技术分享图片
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int M = 2e3+7;
int _,n,m,cas=1;
int f[M],cnt[M];
void init(){
    for(int i=0;i<=n;i++) f[i]=i,cnt[i]=0;
}
int find(int x){
    if(x==f[x]) return x;
    int tmp=f[x];
    f[x]=find(f[x]);
    cnt[x]=(cnt[x]+cnt[tmp])%2;
    return f[x];
}
int main(){
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    scanf("%d",&_);
    while(_--){
        scanf("%d%d",&n,&m);
        init();
        int flg=1;
        while(m--){
            int u,v;
            scanf("%d%d",&u,&v);
            if(u<v) swap(u,v);
            int fu=find(u),fv=find(v);
            if(fu==fv){
                if((cnt[u]-cnt[v]+2)%2!=1) flg=0;
            }
            else{
                f[fu]=fv;
                cnt[fu]=(cnt[v]-cnt[u]+1+2)%2;
            }
        }
        printf("Scenario #%d:\n",cas++);
        if(flg) printf("No suspicious bugs found!\n\n");
        else printf("Suspicious bugs found!\n\n");
    }
    return 0;
}
View Code

 

poj 2492 A Bug's Life

原文:https://www.cnblogs.com/LMissher/p/9643809.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!