poj 2492 A Bug's Life (并查集同食物链)

Language:
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 27157 Accepted: 8841

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


题意:某物种n只,m个描述,表示x,y为异性,判断是否有同性交配。如果发现存在同性交配输出bugs found..

思路:开一个rank数组表示其与父亲节点的关系。rank[x]=0表示x与fa[x]为同性,rank[x]=1,表示x与fa[x]为异性。rank在findset中更新时,很明显可以发现,rank[x]=(rank[x]+rank[fa[x]])%2,不过注意此时fa[x]是未更新前的fa[x]。 在Union操作中,我们需要更新fx,fy,我们发现rank[fy]=(rank[x]+rank[y]+1)%2,rank[fx]=(rank[x]+rank[y]+1)%2,则利用上面的公式更新即可。关于判断,当x,y属于同一个根的时候,如果rank[x]=rank[y],即fx=fy时,我们由rank[x]=rank[y]可推出x和y同性,那么就说明存在bug。详见代码:

  1. #include<cstdio>  
  2. #include<cstring>  
  3. #include<algorithm>  
  4. using namespace std;  
  5. const int MAXN=2000+100;  
  6. int fa[MAXN],rank[MAXN];  
  7. int n,m;  
  8. void init()  
  9. {  
  10.     for(int i=1;i<=n;i++)  
  11.         fa[i]=i;  
  12.     memset(rank,0,sizeof(rank));  
  13. }  
  14. int findset(int x)  
  15. {  
  16.     if(fa[x]!=x)  
  17.     {  
  18.         int root=fa[x];  
  19.         fa[x]=findset(fa[x]);  
  20.         rank[x]=(rank[x]+rank[root])%2;  
  21.     }  
  22.     return fa[x];  
  23. }  
  24. void Union(int x,int y)  
  25. {  
  26.     int fx=findset(x);  
  27.     int fy=findset(y);  
  28.     fa[fy]=fx;  
  29.     rank[fy]=(1+rank[y]+rank[x])%2;  
  30. }  
  31. int main()  
  32. {  
  33.     //freopen("text.txt","r",stdin);  
  34.     int T,kase=0;  
  35.     scanf("%d",&T);  
  36.     while(T--)  
  37.     {  
  38.         scanf("%d%d",&n,&m);  
  39.         kase++;  
  40.         init();  
  41.         int flag=0;  
  42.         for(int i=0;i<m;i++)  
  43.         {  
  44.             int x,y;  
  45.             scanf("%d%d",&x,&y);  
  46.             if(flag)  
  47.                 continue;  
  48.             int fx=findset(x);  
  49.             int fy=findset(y);  
  50.             if(fx==fy)  
  51.             {  
  52.                 if(rank[x]==rank[y])  
  53.                     flag=1;  
  54.             }  
  55.             else  
  56.                 Union(x,y);  
  57.         }  
  58.         printf("Scenario #%d:\n",kase);  
  59.         if(flag)  
  60.             printf("Suspicious bugs found!\n");  
  61.         else  
  62.             printf("No suspicious bugs found!\n");  
  63.         if(T)  
  64.             printf("\n");  
  65.     }  
  66.     return 0;  
  67. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值