HDU 1829 A Bug's Life (分组并查集)

来源:互联网 发布:淘宝代充话费赚钱吗 编辑:程序博客网 时间:2024/04/29 11:44

A Bug's Life

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


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.
 

题目意思:给定一系列数对,例如a和b,表示a和b不是同一种性别,然后不断的给出这样的数对,问有没有性别不对的情况。
例如给定:
1    2
3    4
1    3
那这里就是说1和2不是同种性别,3和4也不是同种性别,1和3不是同种性别,那这样就说明1和3是同一种性别,2和4是同一种性别,所以没有任何歧义,这时候输出No suspicious bugs found!
但是例如,(本文作者:CSDN:凌风)
1 2
2 3
1 3
1和2不同性别,2和3不同性别,那么1和3同一性别的,但是第三组数对又表明1和3不同性别,所以这里就出现了3或者1的性别出现了歧义,也就是说条件矛盾,这时候输出Suspicious bugs found!.

很明显的分组并查集的题目,可以考虑用两种方法来做: (本文作者:CSDN:凌风)
1.开两个并查集,然后合并的时候要合并两次,这样在合并之前判断是否冲突,如果不冲突就进行合并,否则不需要继续合并。
2.开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离,但这里最好取一下余,因为毕竟只有两个分组。
在这里我用两种方法分别实现了一下:

方法一的实现:
#include <cstdio>#include <cstdlib>#include <climits>#include <cstring>#include <algorithm>using namespace std;const int MAX = 2000;int pre[2*MAX+5];bool mark;void init(int n){int i;////(author:CSDN:凌风)for(i=1;i<=MAX+n;++i)pre[i] = i;mark = true;}int root(int x){if(x!=pre[x]){pre[x] = root(pre[x]);}return pre[x];}void merge(int x,int y){int fx,fy;fx = root(x);fy = root(y-MAX);if(fx==fy){mark = false;return;}fy = root(y);if(fx!=fy){pre[fx] = pre[fy];}}int main(){//freopen("in.txt","r",stdin);//(author:CSDN:凌风)int t,i,n,m,x,y,k;scanf("%d",&t);for(i=1;i<=t;++i){scanf("%d %d",&n,&m);init(n);for(k=1;k<=m;++k){scanf("%d %d",&x,&y);if(mark){merge(x,y+MAX);merge(y,x+MAX);}}printf("Scenario #%d:\n",i);if(mark){printf("No suspicious bugs found!\n");}else{printf("Suspicious bugs found!\n");}printf("\n");}return 0;}


方法二的代码:

#include <cstdio>#include <cstdlib>#include <climits>#include <cstring>#include <algorithm>using namespace std;const int MAX = 2000;int pre[MAX+5];int offset[MAX+5];bool mark;void init(int n){int i;//(author:CSDN:凌风)for(i=1;i<=n;++i){pre[i] = i;offset[i] = 0;}mark = true;}int root(int x){int px;if(x!=pre[x]){px = pre[x];pre[x] = root(pre[x]);offset[x] = (offset[px] + offset[x])%2;}return pre[x];}void merge(int x,int y){int fx = root(x);int fy = root(y);if(fx!=fy){pre[fx] = fy;offset[fx] = (1 + offset[y] - offset[x])%2;}root(x);}int main(){//freopen("in.txt","r",stdin);//(author:CSDN:凌风)int t,i,n,m,x,y,k;scanf("%d",&t);for(i=1;i<=t;++i){scanf("%d %d",&n,&m);init(n);for(k=1;k<=m;++k){scanf("%d %d",&x,&y);if(mark){merge(x,y);}if(offset[x]==offset[y]){mark = false;}}printf("Scenario #%d:\n",i);if(mark){printf("No suspicious bugs found!\n");}else{printf("Suspicious bugs found!\n");}printf("\n");}return 0;}



5 2
原创粉丝点击