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 4Sample Output
Scenario #1:Suspicious bugs found!Scenario #2:No suspicious bugs found!ps:基础的关系并查集,用rank[i]来表示i与根节点的关系,rank[]=1表示同性,rank[]=0表示异性 然后列举所有的情况,推出rank[]的路径压缩表达式和判断条件表达式即可代码:#include<stdio.h>#include<string.h>#define maxn 2000+10int pre[maxn],rank[maxn];int flag;void init(){ for(int i=1; i<=maxn; i++) { pre[i]=i; rank[i]=1;//自己与自己的关系为同性 } flag=0;}int Find(int x){ int temp=pre[x]; if(x==pre[x]) return x; pre[x]=Find(temp); rank[x]=(rank[x]+rank[temp]+1)&1; return pre[x];}void join(int x,int y){ int fx=Find(x),fy=Find(y); if(fx!=fy) { pre[fx]=fy; rank[fx]=(rank[x]+rank[y])&1; } else { if(!((rank[x]+rank[y])&1))//如果rank[x]和rank[y]同时为(1,1)或(0,0)时,说明不符合条件 flag=1; }}int main(){ int t,k=1,n,m; scanf("%d",&t); while(k<=t) { scanf("%d%d",&n,&m); init(); int u,v,i; for(i=1; i<=m; i++) { scanf("%d%d",&u,&v); join(u,v); } printf("Scenario #%d:/n",k++); if(flag) printf("Suspicious bugs found!/n"); else printf("No suspicious bugs found!/n"); puts(""); } return 0;}
新闻热点
疑难解答