A Bug's Life poj 2492

http://acm.hust.edu.cn/vjudge/contest/121379#problem/J

题意:让你看是否有同性恋,比如a和b,b和c,c和a分别交配,那么肯定产生了同性恋。规定同性恋是0,非同性恋是1。

完全模仿食物链的代码。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
#define maxn 2100
int father[maxn], r[maxn];

int Find(int x)
{
    if(x!=father[x])
    {
        int k = father[x];
        father[x] = Find(father[x]);
        r[x] = (r[x]+r[k])%2;
    }
    return father[x];
}

int main()
{
    int T, n, m, a, b,t=1;

    scanf("%d", &T);
    while(T--)
    {
        int flag = 0;
        scanf("%d %d", &n, &m);

        for(int i=0; i<=n; i++)
            father[i] = i;

        memset(r, 0, sizeof(r));

        while(m --)
        {
            scanf("%d %d", &a, &b);

            if(flag)  continue;

            int ra = Find(a);
            int rb = Find(b);


            if(ra == rb && (r[b]+1)%2!=r[a]) flag=1;

            else if(ra!=rb)
            {
                father[ra]=rb;
                r[ra]=(1-r[a]+r[b]+2)%2;
            }
        }

            if(t != 1)printf("
");
        printf("Scenario #%d:
", t++);
        if(flag)printf("Suspicious bugs found!
");
        else printf("No suspicious bugs found!
");
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/daydayupacm/p/5719434.html