HDU1829- A Bug's Life

来源:互联网 发布:mac怎么把两个窗口 编辑:程序博客网 时间:2024/06/02 00:22


A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15661    Accepted Submission(s): 5124


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
23 31 22 31 34 21 23 4
 

Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!
Hint
Huge input,scanf is recommended.
 
类似于POJ1182—食物链 ,思路相同,此题将并查集扩大2倍,设有a个虫子,per数组1到a表示一个性别,a+1到2a表示一个性别,每输入两个虫子x,y判断x和y是否同类,x+a和y+a是否同类若x,y属于不同性别,合并(x,y+a)与(x+a,y)。
#include<stdio.h>#define maxn 4020int per[maxn],rank[maxn];int find(int x){    int r=x;    while(per[r]!=r)    {        r=per[r];    }    int i=x,j;    while(i!=r)    {        j=per[i];        per[i]=r;        i=j;    }    return r;}void join(int x,int y){    x=find(x);    y=find(y);    if(x==y)        return;    if(rank[x]<rank[y])    {       per[x]=y;    }    else    {        per[y]=x;        if(rank[x]==rank[y])            rank[x]++;    }}bool same(int x,int y){    if(find(x)==find(y))        return true;    else        return false;}int main(){    int n,i;    scanf("%d",&n);    for(i=1;i<=n;i++)    {        int a,b,flag,j;        scanf("%d%d",&a,&b);        for(j=1;j<=2*a;j++)        {            per[j]=j;            rank[j]=1;        }        flag=0;        while(b--)        {            int x,y;            scanf("%d%d",&x,&y);            if(same(x,y)||same(x+a,y+a))            {                flag=1;            }            else            {                join(x,y+a);                join(x+a,y);            }        }        printf("Scenario #%d:\n",i);        if(flag==1)            printf("Suspicious bugs found!\n");        else            printf("No suspicious bugs found!\n");        printf("\n");    }}
原创粉丝点击