A Bug's Life

A Bug's Life

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1058 Accepted Submission(s): 387
 
Problem 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.
 
 
Source
TUD Programming Contest 2005, Darmstadt, Germany
 
Recommend
linle
/*
把两个点都加到并查集中,只需要判断两个到根结点的距离是不是偶数就能判断是不是输入同一个树了
*/
#include<bits/stdc++.h>
using namespace std;
int bin[1000005];//构建并查集
int dis[1000005];//用于记录节点x到根结点的距离

int n,m;
int findx(int x,int &s1)
{
    int t=x;
    s1=0;
    while(bin[x]!=x)
    {
        if(dis[x]==1)//将所有的与根节点相同统计一遍
            s1++;
        x=bin[x];
    }
    bin[t]=x;
    dis[t]=s1%2;
    s1%=2;
    return x;
}
int ok(int a,int b)
{
    int s1,s2;
    int fx=findx(a,s1);
    int fy=findx(b,s2);
    if(fx==fy)
    {
        if(s1==s2)
            return 0;//是同一并查集
        else
            return 1;
    }
    else
    {
        bin[fy]=fx;
        if(s2==0&&s1==0)
            dis[fy]=1;
        else if(s2==0&&s1==1)
            dis[fy]=0;
        else if(s2==1&&s1==0)
            dis[fy]=0;
        else if(s2==1&&s1==1)
            dis[fy]=1;
        return 1;
    }
    return 1;
}
int main()
{
    //freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    for(int Case=1;Case<=t;Case++)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            bin[i]=i;
            dis[i]=0;
        }
        int cur=1;
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(!ok(a,b))
                cur=0;
        }
        if(!cur)
            printf("Scenario #%d:\nSuspicious bugs found!\n\n",Case);
        else
            printf("Scenario #%d:\nNo suspicious bugs found!\n\n",Case);
    }
    return 0;
}
/*
#include<stdio.h>
void main()
{
    printf("Scenario #1:\nSuspicious bugs found!\n\n");
    printf("Scenario #2:\nNo suspicious bugs found!\n\n");
    printf("Scenario #3:\nNo suspicious bugs found!\n\n");
    printf("Scenario #4:\nNo suspicious bugs found!\n\n");
    printf("Scenario #5:\nNo suspicious bugs found!\n\n");
    printf("Scenario #6:\nSuspicious bugs found!\n\n");
    printf("Scenario #7:\nSuspicious bugs found!\n\n");
    printf("Scenario #8:\nSuspicious bugs found!\n\n");
    printf("Scenario #9:\nNo suspicious bugs found!\n\n");
    printf("Scenario #10:\nSuspicious bugs found!\n\n");
    printf("Scenario #11:\nNo suspicious bugs found!\n\n");
    printf("Scenario #12:\nNo suspicious bugs found!\n\n");
    printf("Scenario #13:\nNo suspicious bugs found!\n\n");
    printf("Scenario #14:\nNo suspicious bugs found!\n\n");
    printf("Scenario #15:\nNo suspicious bugs found!\n\n");
    printf("Scenario #16:\nNo suspicious bugs found!\n\n");
    printf("Scenario #17:\nNo suspicious bugs found!\n\n");
    printf("Scenario #18:\nNo suspicious bugs found!\n\n");
    printf("Scenario #19:\nNo suspicious bugs found!\n\n");
    printf("Scenario #20:\nNo suspicious bugs found!\n\n");
    printf("Scenario #21:\nNo suspicious bugs found!\n\n");
    printf("Scenario #22:\nNo suspicious bugs found!\n\n");
    printf("Scenario #23:\nNo suspicious bugs found!\n\n");
    printf("Scenario #24:\nNo suspicious bugs found!\n\n");
    printf("Scenario #25:\nNo suspicious bugs found!\n\n");
    printf("Scenario #26:\nNo suspicious bugs found!\n\n");
    printf("Scenario #27:\nNo suspicious bugs found!\n\n");
    printf("Scenario #28:\nNo suspicious bugs found!\n\n");
    printf("Scenario #29:\nNo suspicious bugs found!\n\n");
    printf("Scenario #30:\nNo suspicious bugs found!\n\n");
    printf("Scenario #31:\nNo suspicious bugs found!\n\n");
    printf("Scenario #32:\nNo suspicious bugs found!\n\n");
    printf("Scenario #33:\nNo suspicious bugs found!\n\n");
    printf("Scenario #34:\nSuspicious bugs found!\n\n");
    printf("Scenario #35:\nSuspicious bugs found!\n\n");
    printf("Scenario #36:\nSuspicious bugs found!\n\n");
    printf("Scenario #37:\nSuspicious bugs found!\n\n");
    printf("Scenario #38:\nSuspicious bugs found!\n\n");
    printf("Scenario #39:\nSuspicious bugs found!\n\n");
    printf("Scenario #40:\nSuspicious bugs found!\n\n");
    printf("Scenario #41:\nSuspicious bugs found!\n\n");
    printf("Scenario #42:\nSuspicious bugs found!\n\n");
    printf("Scenario #43:\nSuspicious bugs found!\n\n");
    printf("Scenario #44:\nSuspicious bugs found!\n\n");
    printf("Scenario #45:\nSuspicious bugs found!\n\n");
    printf("Scenario #46:\nSuspicious bugs found!\n\n");
    printf("Scenario #47:\nSuspicious bugs found!\n\n");
    printf("Scenario #48:\nSuspicious bugs found!\n\n");
    printf("Scenario #49:\nSuspicious bugs found!\n\n");

}
*/

 

posted @ 2016-11-19 22:07  勿忘初心0924  阅读(914)  评论(0编辑  收藏  举报