A Bug's Life

Background
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
Input
The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.
Output
The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.
Sample Input
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
Scenario #1:
Suspicious bugs found!
 
Scenario #2:
No suspicious bugs found!
Hint
Huge input,scanf is recommended.
 
这一题类似于食物链,是一道并查集的应用,思路是:先找出第一个数(a)与它根节点(B)的关系,然后找第二个数(b)与它根节点(B)的关系;在判断A==B,(1)如果相等就求出a与b的关系,然后判断;(2)如果不相等就把A和B连成一棵树;
 
用异或进行运算比较简单;
另外,这是我的参考:http://blog.csdn.net/pure_life/article/details/2922118;
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define N 2010
#define INF 0x3f3f3f3f

using namespace std;
int fa[N];
bool flag[N];

int Find(int x, bool &s)
{
    int h;
    if(fa[x]==x)
    {
        h=x;
        s=false;
    }
    else
    {
        h=Find(fa[x], s);
        fa[x]=h;
        s=flag[x]^s;
        flag[x]=s;
    }
    return h;
}

int main()
{
    int i, j, a, b, t, n, m, T, A, B;
    bool f, sa, sb;
    scanf("%d", &T);
    for(t=1;t<=T;t++)
    {
        scanf("%d%d", &n, &m);
        for(i=0;i<=n;i++)
        {
            fa[i]=i;
            flag[i]=false;
        }
        f=true;
        for(i=0;i<m;i++)
        {
            scanf("%d%d", &a, &b);
            if(f)
            {


                A=Find(a, sa=false);
                B=Find(b, sb=false);
                if(A==B)
                {
                    f=sa^sb;
                }
                else
                {
                    fa[A]=B;
                    flag[A]=!(sa^sb);
                }
            }
        }
        printf("Scenario #%d:\n", t);
        if (f)
        {
            printf("No suspicious bugs found!\n\n");
        }
        else
        {
            printf("Suspicious bugs found!\n\n");
        }
    }
    return 0;
}

 

posted on 2017-07-14 09:33  小春天  阅读(191)  评论(0编辑  收藏  举报

导航