A Bug's Life --- 种类并查集

来源:互联网 发布:canbot 软件 编辑:程序博客网 时间:2024/06/05 20:05
Time Limit: 10000MS
Memory Limit: 65536KTotal Submissions: 27710
Accepted: 9010

Description

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

23 31 22 31 34 21 23 4

Sample Output

Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany

解题分析:

此题的重点是在如何推出相关的关系公式,找到其种类的关系。
可以看出只要两个虫子序号之差是奇数说明就是同性。

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 2005
bool flag;
int fx, fy, n;
int pre[MAXN], relate[MAXN];


int Find(int x)
{
    if(pre[x] == -1)
        return x;
    else
    {
        int tmp = pre[x];
        pre[x] = Find(pre[x]);
        relate[x] = (relate[x]+relate[tmp])%2; 
        return pre[x];
    }
}


int main()
{
    int T, m, a, b;
    scanf("%d", &T);
    for(int k = 1; k <= T; ++k)
    {
        flag = true;
        scanf("%d%d", &n, &m);
        {
            memset(pre, -1, (n+1)*sizeof(int));
            memset(relate, 0, (n+1)*sizeof(int));
            while(m--)
            {
                scanf("%d%d", &a, &b);
                if(!flag)
                    continue;
                fx = Find(a);
                fy = Find(b);
                
                if(fx != fy)
                {
                    pre[fx] = fy;
                    relate[fx] = (relate[a] + relate[b] + 1)%2;
                }
                else
                {
                    if(relate[a] == relate[b])
                        flag = false;
                }
                
            }
            
            printf("Scenario #%d:\n", k);
            if(flag)   
                printf("No suspicious bugs found!\n\n");
            else
                printf("Suspicious bugs found!\n\n");                  
        }
    }
    return 0; 
}


0 0