寒假训练营之并查集:A Bug's Life

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.

题意: 输入互相交配的昆虫,找是否存在同性恋的昆虫,其中有多个案例

我想说的是,我现在是越来越懵逼了。。。
同样这道题也是一道并查集的题,除了理解并查集之外,这题我还踩了一个坑,就是输出时要注意:
在这里插入图片描述
在每个输出案例时,两个之间有一个空格,不注意就会 Presentation Error

#include<iostream>
#include<cstdio>
using namespace std;
int parent[1000010];
int total[1000010];
int flag;
int getParent(int a)
{
    if(a==parent[a])
        return a;
    else
    {
        int t=getParent(parent[a]);
        total[a]=(total[a]+total[parent[a]])%2;
        return parent[a]=t;
    }
    /*if(a!=parent[a])              //这里一直没搞懂,不知道这样为啥不可以AC,一直WA
    {
        parent[a]=getParent(parent[a]);
        total[a]=(total[a]+total[parent[a]])%2;       //这里我现在都没搞太懂
    }
    return parent[a];*/
}
void Merge(int a,int b)
{
    int p1=getParent(a);
    int p2=getParent(b);
    if(p1==p2)
    {
        if(total[a]==total[b])
            flag=0;
    }
    else
    {
        parent[p2]=p1;
        total[p2]=(total[a]-total[b]+1)%2;
    }
}
int main()
{
    int t,num=0;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            parent[i]=i;
            total[i]=0;
        }
        flag=1;
        int x,y;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            Merge(x,y);
        }
        printf("Scenario #%d:\n",++num);
        if(flag==1)
            printf("No suspicious bugs found!\n\n");                 //这里就是我感觉的一个坑点,需要两个换行符
        else
            printf("Suspicious bugs found!\n\n");
    }
    return 0;
}

小白心得

猜你喜欢

转载自blog.csdn.net/boliu147258/article/details/87447825