Fork me on GitHub

POJ 2492 A Bug's Life

这题跟 POJ 1703 Find them and Catch them 是一样一样的,就不讲解了,不懂可以看

http://www.cnblogs.com/whatbeg/p/3498416.html

 

代码:

#include <iostream>
#include <cstdio>
using namespace std;
#define N 2100

int fa[N],sa[N];

void makeset(int n)
{
    for(int i=1;i<=n;i++)
    {
        fa[i] = i;
        sa[i] = 0;  // 0 : same   1 : different
    }
}

int findset(int x)
{
    if(x != fa[x])
    {
        int tmp = fa[x];
        fa[x] = findset(fa[x]);
        sa[x] = (sa[tmp] + sa[x]) % 2;
    }
    return fa[x];
}

void unionset(int a,int b)
{
    int x = findset(a);
    int y = findset(b);
    if(x == y)
        return;
    fa[x] = y;
    sa[x] = (sa[a] + sa[b] + 1)%2;
    return;
}

int main()
{
    int t,n,m,i;
    int a,b;
    int cs = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        makeset(n);
        int flag = 1;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            if(findset(a) == findset(b))
            {
                if(sa[a] == sa[b])
                {
                    flag = 0;
                }
            }
            else
                unionset(a,b);
        }
        printf("Scenario #%d:\n",cs++);
        if(flag)
            cout<<"No suspicious bugs found!"<<endl;
        else
            cout<<"Suspicious bugs found!"<<endl;
        cout<<endl;
    }
    return 0;
}
View Code

 

posted @ 2014-01-03 19:13  whatbeg  阅读(228)  评论(0编辑  收藏  举报