hdoj-1289-A Bug's Life【种类并查集】

A Bug‘s Life

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11386 Accepted Submission(s): 3709

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

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

Recommend

linle | We have carefully selected several similar problems for you:
1558 1811 1325 2473 3172

#include<stdio.h>
int root[2010];
int rank[2010];
int find_set(int x){
	if(root[x]==x) return x;
	int t=root[x];
	root[x]=find_set(t);
	rank[x]=(rank[x]+rank[t])%2;
	return root[x];
}
void unio(int a,int b){
	int  t,k;
	t=root[a];
	k=root[b];
	root[t]=k;
	rank[t]=(rank[b]+1-rank[a]);
	return ;
}
int main(){
	int t,cas=0;;
	scanf("%d",&t);
	while(t--){
		printf("Scenario #%d:\n",++cas);
		int n,m,i,ok=0,a,b;
		scanf("%d%d",&n,&m);
		for(i=0;i<=n;++i){
			root[i]=i;
			rank[i]=0;
		}
		for(i=0;i<m;++i){
			scanf("%d%d",&a,&b);
			if(!ok){
				int pa,pb;
				pa=find_set(a) ;
				pb=find_set(b);
				if(pa==pb&&(rank[a]==rank[b])){
					ok=1;
				}
				else
				unio(a,b);
			}
		}
		if(ok)
		printf("Suspicious bugs found!\n\n");
		else
		printf("No suspicious bugs found!\n\n");

	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdoj-1289-A Bug's Life【种类并查集】

时间: 2024-10-11 22:00:12

hdoj-1289-A Bug's Life【种类并查集】的相关文章

hdoj 1829 A bug&#39;s life 种类并查集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有两种性别,每次m次操作,每次给出(a,b),如果a和b是同性别就出现了错误,也就是说出现了判断它有两种性别的错误.我的策略同样是两个集合,用并查集维护两个集合之间的关系.具体证明请看我的上一篇博客,关于这种做法的正确性的证明. 代码如下: 1 #include<bits/stdc++.h> 2 t

HDU 1829 &amp;&amp; POJ 2492 A Bug&#39;s Life(种类并查集)

题目地址:HDU 1829     POJ 2492 这个题可以用两种方法做,第一眼看完题是觉得用dfs染色判断二分图.然后又写的刚学的种类并查集.原来并查集可以这样用,真是神奇.. dfs染色代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #incl

【POJ】2492 A bug&#39;s life ——种类并查集

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

hdu1829A Bug&#39;s Life(种类并查集)

传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 1 #include<bits/stdc++.h> 2 using namespace std; 3 int f[2005],n,m,rankk[2005]; 4 bool flag; 5 inline void init() 6 { 7 flag=false; 8 for(int i=0; i<=n; ++i)

poj 2492 a bug&#39;s life 简单种类并查集

题意大致为找同性恋的虫子.... 这个比食物链要简单些.思路完全一致,利用取余操作实现关系之间的递推. 个人感觉利用向量,模和投影可能可以实现具有更加复杂关系的并查集. 1 #include<cstdio> 2 using namespace std; 3 const int MAXN=50010; 4 int fa[MAXN]; 5 int rel[MAXN]; // 0代表同类,1代表吃fa[i],2代表被吃 6 void _set(int n) 7 { 8 for(int i=1;i&l

hdoj-1289-A Bug&amp;#39;s Life【种类并查集】

A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11386 Accepted Submission(s): 3709 Problem Description Background Professor Hopper is researching the sexual behavior of a rare specie

hdu1829 A Bug&#39;s Life 基础种类并查集

题目的大意可以理解为:A爱B,B爱C ……给出一系列爱恋的关系,推断有没有同性恋. 思路是把相同性别的归为一个集合,异性的异性为同性. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2010; int f[N], r[N], flag; void init() { for(int i=1;i&

hdu 1829 A Bug&#39;s Life (基础种类并查集)

先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组,一个存并查集内的父子关系,一个存各个节点所属的种类关系. 以这道题为例(题意在后面,如果没有读题,可以先看完题在来看这部分)—— 这道题很明显,将bug分成两类,一公一母.但是实际上我们并不关心它是公的还是母的,只关心它们之间是同性还是异性.所以,我们可以设与并查集的根节点同性的为0,反之为1.所

A Bug&#39;s Life(种类并查集)(也是可以用dfs做)

http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1829 Description Background Professor Hopper is researching the sexual behavior of a r