(并查集)A Bug's Life -- POJ -- 2492

链接:

http://poj.org/problem?id=2492

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#problem/J

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

#define N 10005

int f[N], r[N];

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

    return f[x];
}


int main()
{
    int i, t, n, m, k=1;
    scanf("%d", &t);

    while(t--)
    {
        int a, b, fa, fb, flag=0;
        scanf("%d%d", &n, &m);

        for(i=0; i<=n; i++)
        {
            f[i]=i;
            r[i]=0;
        }


        for(i=0; i<m; i++)
        {
            scanf("%d%d", &a, &b);
            fa=Find(a);
            fb=Find(b);
            if(fa!=fb)
            {
                f[fa]=fb;
                r[fa]=(r[b]-r[a]+1)%2;
            }
            else
            {
                if(r[a]==r[b])
                   flag=1;
            }

        }

        printf("Scenario #%d:
", k++);
        if(flag)
            printf("Suspicious bugs found!

");
        else
            printf("No suspicious bugs found!

");
    }

    return 0;
}
勿忘初心
原文地址:https://www.cnblogs.com/YY56/p/4735852.html