HDU1829

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.

题意:判断是否有gay

思路:将数组扩大一倍,(0,n)范围表示一个性别,(n,2n)表示另一个性别,如果两个虫子搞一起了(假设编号为bug1和bug2),就将(bug1,bug2+n)并到一个集合,(bug1+n,bug2)并到一个集合,如果有两只虫子在同一个集合里,则说明有gay,后面的就不需要操作了,只需要读入数据即可。

(还有其他解法,在开一个数组维护每个结点和根结点的关系,但是没看懂就没用那种方法,以后看懂了再来补上)

#include<iostream>
#include<cstdio>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1000000 + 50;
int p[maxn*2];

void maketree(int n)
{
    for (int i = 1; i <= maxn*2; i++)
    {
        p[i] = i;
    }
}
int Find(int x)
{
    if (x != p[x])
    {
        p[x] = Find(p[x]);
    }
    return p[x];
}
void unite(int x, int y)
{
    int p1 = Find(x);
    int p2 = Find(y);
    p[p2] = p1;
}

int main()
{
    int T;
    scanf("%d", &T);
    int k = 1;
    while (T--)
    {
        int n, m;
        scanf("%d %d", &n, &m);
        maketree(n);
        bool flag = false;
        for (int i = 0; i < m; i++)
        {
            int bug1, bug2;
            scanf("%d %d", &bug1, &bug2);
            int p1 = Find(bug1);
            int p2 = Find(bug2);
            if (flag)
                continue;

            if (p1 == p2)   //ÔÚͬһ×é
                flag = true;
            else
            {
                unite(p1, p2+n);
                unite(p2, p1+n);
            }
        }
        if (flag)
            printf("Scenario #%d:\nSuspicious bugs found!\n\n", k++);
        else
            printf("Scenario #%d:\nNo suspicious bugs found!\n\n", k++);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39479426/article/details/81612998