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

要求根据M对交配关系找出N个虫子中是否存在同性恋/...

和上题一样...T_T

/*
Problem ID:I - A Bug's Life
meaning: 寻找同性恋
Analyzing:种类并查集,bug:1,2;1,2.。。T_T
*/
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>

using namespace std;
typedef struct even{int pi,di;}even;

#define FOR(i,s,t) for(int i=(s); i<(t); i++)
#define BUG puts("here!!!")
#define print(x) printf("%d\n",x)
#define LL long long
#define maxn 2005
int pre[maxn],rela[maxn];
void init(){
    FOR(i,0,maxn){pre[i]=-1;rela[i]=0;}
}
int find(int x){
    int s=x;
    if(pre[x]<0) return x;
    s=find(pre[x]);
    rela[x]=rela[x]^rela[pre[x]];
    return pre[x]=s;
}
void Union (int R1,int R2){
    int r1=find(R1);
    int r2=find(R2);
    if(r1==r2) return;
    pre[r1]=r2;
    rela[r1]=~(rela[R1]^rela[R2]);

}
int main(){
    int T,N,M;
    int a,b,flag,Cas=1;
    scanf("%d",&T);
    while(T--){
        flag=1;
        init();
        scanf("%d%d",&N,&M);
        while(M--){
            scanf("%d%d",&a,&b);
            if(!flag) continue;
            if(find(a)==find(b)){
                if(rela[a]==rela[b])
                    flag=0;
            }
            else
                Union(a,b);
        }
        printf("Scenario #%d:\n",Cas++);
        if(!flag) printf("Suspicious bugs found!\n");
        else printf("No suspicious bugs found!\n");
        puts("");
    }
    return 0;
}


你可能感兴趣的:(struct,bugs)