hdu 1829 A Bug's Life

来源:互联网 发布:知乎封号了怎么办 编辑:程序博客网 时间:2024/06/05 13:53

A Bug's Life

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 30   Accepted Submission(s) : 12

Font: Times New Roman | Verdana | Georgia

Font Size:  

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!

该题是一个并查集的升级,对于我这个渣渣学并查集的,真的挺难,我看了一系列的博客和学长的论文,然后才有了理解,首先,还是原来

的那样,有一个并查集的模版。每个元素属于一个集合,还有得设一个深度的数组,表示它和原先的父类的距离,目的是后面判断是否为同性恋

。然后是判断结点的问题,当父类相同的情况下,判断深度,若深度一样,则为同性恋。否则的话,将两个集合合并, 合并的时候得注意一点,

不但集合要合并,而且他们的偏移量也要合并。0 0 就成为了1 ,,0 1 就成了0 1 1 就成了1 所以总结下来 就是 r=(r1+r2+1)%2;

然后我们用了路径压缩,所以在路径压缩的时候,也得进行偏移量的改变。最后还得控制一下格式的输出。

#include<stdio.h>#include<string.h>#define maxn 1000010int s[maxn];int r[maxn];int biaoji[maxn];int fu(int x){if(x==s[x])return s[x];int t=fu(s[x]);r[x]=(r[x]+r[s[x]])%2;  //压缩路径的偏移量改变 s[x]=t;return s[x];}int pan(int a,int b){int x=fu(a);int y=fu(b);if(x==y){if(r[a]==r[b])  //同性恋return 1;}else{s[x]=y;     //合并集合 r[x]=(r[a]+r[b]+1)%2;  // 改变偏移量 }return 0;}int main(){int bbs;int n,m,i,j,k=1,a,b;scanf("%d",&bbs);for(j=1;j<=bbs;j++) {int q=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){s[i]=i;r[i]=0;}for(i=1;i<=m;i++){scanf("%d%d",&a,&b);if(pan(a,b)){q=1;}}if(q==1){printf("Scenario #%d:\n",k);printf("Suspicious bugs found!\n\n");}else {printf("Scenario #%d:\n",k);printf("No suspicious bugs found!\n\n");}k++;}return 0;}


0 0
原创粉丝点击