A Bug's Life

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

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.

题解:种类并查集,假设之前输入的两个放在一起的虫子不是同性恋,那么他们就是互为异性,分别将他们加入两个不同的集合,每次输入新的一组虫子时,先查询他们在不在同一集合,若在,则为同性恋,否则分别将两个虫子所在的集合与其已存过的或者根节点的异性所在的集合合并,直到最后。

代码如下:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<deque>
using namespace std;
typedef long long ll;
const int Max=2005;
int p[Max];
int op[Max];//代表每个虫子的配对异性
int rank[Max];
int findx(int x)
{
    int r=x;
    while(p[r]!=r)
    {
        r=p[r];
    }
    return r;
}
void union_set(int x,int y)
{
    int fx=findx(x);
    int fy=findx(y);
    if(fx==fy)
        return;
    else//初始化并查集
    {
        if(rank[fx]>rank[fy])
        {
            p[fy]=fx;
        }
        else
        {
            p[fx]=fy;
            if(rank[fx]==rank[fy])
                rank[fy]++;
        }
    }
}
int main()
{
    int t,s=1;
    cin>>t;
    while(t--)
    {
        int k=0;
        memset(rank,0,sizeof(rank));//初始化并查集
        memset(op,0,sizeof(op));
        for(int i=0;i<Max;i++)
        {
            p[i]=i;
        }
        int num,casf;
        scanf("%d%d",&num,&casf);
        for(int i=0;i<casf;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(findx(a)==findx(b)) //如果这两个虫子在同一个集合里找到 就是同性恋
                k=1;
            if(op[a]==0&&op[b]==0) //代表两个虫子都没被分类过,储存下每个虫子的异性
            {
                op[a]=b;
                op[b]=a;
            }
            else if(op[a]==0) //如果a没被分类过,将它的异性记录为b,并将它加入b的异性所在的集合
            {
                op[a]=b;
                union_set(a,op[b]);
            }
            else if(op[b]==0)//同上
            {
                op[b]=a;
                union_set(b,op[a]);
            }
            else //否则合并a与op[b]所在的集合以及b与op[a]所在的集合
            {
                union_set(a,op[b]);
                union_set(b,op[a]);
            }
        }
        printf("Scenario #%d:\n",s++);
        if(k)
            printf("Suspicious bugs found!\n");
        else
            printf("No suspicious bugs found!\n");
        if(t)
			printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/baiyi_destroyer/article/details/80444111