HDU 1829

这道题 可以用二分图染色来做 ,也可以用带权并查集来做
就是向量并查集 我们增加一个rea数组 其中 为0时表示这个节点和父亲节点的关系是异性 1为同性

我们每次合并他们的关系之前 判断它们在同一树上时 rea数组的值是否相等 相等说明它们与父亲节点关亲一样 说明Suspicious bugs found! 否则我们合并后继续

#include<cstdio>
#include<cstring>
int fa[2005];
int rea[2005];
struct note
{
    int x,y;
} a[1000005];

int finds(int x){
    if(x==fa[x])
        return x;
    int t=fa[x];
    fa[x]=finds(fa[x]);
    rea[x]=(rea[x]+rea[t]+1)%2;
    return fa[x];
}

int main(){
    int T;
    scanf("%d",&T);
    int coun=1;
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++){
            fa[i]=i;
            rea[i]=1;
        }
        int top=0;
        while(m--){
            scanf("%d%d",&a[top].x,&a[top].y);
            top++;
        }
        bool ans=true;
        for(int i=0; i<top; i++)
        {
            int x=a[i].x,y=a[i].y;
            int tx=finds(x);
            int ty=finds(y);
            if(!(tx^ty))
            {
                if(rea[x]==rea[y])
                {
                    ans=false;
                    break;
                }
            }
            fa[tx]=ty;
            rea[tx]=(-rea[x]+rea[y]+2)%2;
        }
        if(!ans)
            printf("Scenario #%d:\nSuspicious bugs found!\n",coun++);
        else
            printf("Scenario #%d:\nNo suspicious bugs found!\n",coun++);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(HDU 1829)