POJ 2492 A Bug's Life

来源:互联网 发布:怎么删除知乎的提问 编辑:程序博客网 时间:2024/06/03 13:08

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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

并查集。然而我超时了······

要输出两个换行符······


这个是超时的代码······

#include<cstdio>#include<cstring>int t,n,m,fa[2006],k,x,y;bool b[2006];int findd(int u){if(fa[u]==u) return u;return findd(fa[u]);}int qiou(int u,int kk){if(fa[u]==u) return kk%2;return qiou(fa[u],(++kk)%2);}int main(){scanf("%d",&t);for(int u=1;u<=t;u++){scanf("%d%d",&n,&m);  //n虫子,m组关系 k=0;memset(b,0,sizeof(b));for(int i=1;i<=n;i++)  fa[i]=i;for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);if(!b[x] && !b[y]) b[x]=b[y]=1,fa[y]=x;if(b[x] && !b[y]) fa[y]=x,b[y]=1;if(b[y] && !b[x]) fa[x]=y,b[x]=1;if(b[x] && b[y]) {if(findd(x)==findd(y))  //在同一棵树中   if(qiou(x,1)==qiou(y,1))  {k=1;break;  } if(findd(x)!=findd(y)) fa[x]=y;  //不在同一棵树中 }}printf("Scenario #%d:\n",u);if(k) printf("Suspicious bugs found!\n");else printf("No suspicious bugs found!\n");}return 0;}


然而它改起来太麻烦了······ 于是我又换了一种方法······ 参考图神的代码······

这种方法感觉好神奇啊······

#include<cstdio>#include<cstring>int t,n,m,fa[4006],k,x,y,d[4006];inline int pan(int u){if(fa[u]==u){return u;}return pan(fa[u]);}int main(){scanf("%d",&t);for(int u=1;u<=t;u++){scanf("%d%d",&n,&m);  //n虫子,m组关系 k=0;for(int i=1;i<=2*n;i++)  fa[i]=i;for(int i=1;i<=m;i++){scanf("%d%d",&x,&y);if(pan(x)==pan(y)) k=1;else{fa[pan(x+n)]=pan(y);fa[pan(x)]=pan(y+n);}}printf("Scenario #%d:\n",u);if(k) printf("Suspicious bugs found!\n\n");else printf("No suspicious bugs found!\n\n");}return 0;}


1 0
原创粉丝点击