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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值