hdu1829

来源:互联网 发布:靠谱淘宝 外贸 知乎 编辑:程序博客网 时间:2024/05/18 01:22

链接:点击打开链接

题意:N个人,编号分别为1~N,有K条信息,每条信息含有两个数代表编号为这两个数的人的性别不同,因此后面信息有可能与前面的信息产生矛盾,问是否产生矛盾

代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int father[5005];int found(int x){    if(x!=father[x])    father[x]=found(father[x]);    return father[x];}void unite(int x,int y){    x=found(x);    y=found(y);    if(x==y)    return;    father[x]=y;}bool same(int x,int y){    return found(x)==found(y);}                                           //并查集模板int main(){    int i,j,p,t,n,k,x,y,ans;    scanf("%d",&t);    for(p=1;p<=t;p++){        ans=0;x        scanf("%d%d",&n,&k);        for(i=1;i<=2*n;i++)        father[i]=i;        while(k--){            scanf("%d%d",&x,&y);            if(same(x,y)||same(x+n,y+n))    //x,y同性时不符合情况            ans++;            else{                unite(x,y+n);                unite(x+n,y);            }        }        printf("Scenario #%d:\n",p);        if(!ans)        printf("No suspicious bugs found!\n\n");        else        printf("Suspicious bugs found!\n\n");    }    return 0;}

0 0
原创粉丝点击