hdu1829 A Bug's Life (并查集)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829

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!

题目大意:Hopper教授正在研究一种稀有虫子的交配行为。他假设它们有两种性别并且它们只与异性交配。在他的实验中,很容易识别虫子和它们的交配行为,因为虫子背后印有编号。

问题

给定一组虫子的交配行为,确定实验是支持教授的假设即虫子没有同性恋,还是有部分交配行为不符合假设。

这道题实际上还是并查集,和以往不同的是。以往给出两个元素的关系,然后认为这两个元素有联系,把它们划归为同一个集合。   最后看看能划分出多少个不同的集合。 本道题,可以认为有两个集合,异性和同性。  给出关系,希望你检查这两个集合有没有连接,即有没有同性恋。

大体思路:   ‘合并’操作,  ‘查找’操作不变。设置一个sex[]   数组,sex[i]存储与 i  性别相反的对象编号 ,初始化为0 。 然后将于i  发生联系的元素合并为同一个集合(因为人为它们是同性)。下次输入x,y时,如果      find(x)==fidn(y),我们就可以人为出现了反常行为。

总之,就是将同性的化为同一个集合中去。

代码如下:

#include <cstdio>
#include <cstring>
#define N 2017
int sex[N], set[N];
int find(int x)
{
    return x == set[x]?x:set[x]=find(set[x]);
}
void Union(int x, int y)
{
    int f1 = find(x);
    int f2 = find(y);
    if(f1 != f2)
    {
        set[f2] = f1;
    }
}
void init(int n)
{
    int i;
    for(i = 1; i <= n; i++)
    {
        set[i] = i;
        sex[i] = 0;
    }
}

int main()
{
	int n, m;
    int t, i, j;
    int x, y, cas = 0, flag;
    scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		init(n);
		flag = 0;
		for(i = 0; i < m; i++)
		{
			scanf("%d%d",&x,&y);
			if(flag)
				continue;
			if(find(x) == find(y))
			{
				flag = 1;
				continue;
			}
			if(sex[x] == 0)
				sex[x] = y;
			else
				Union(sex[x],y);
			if(sex[y] == 0)
				sex[y] = x;
			else
				Union(sex[y],x);
		}
		printf("Scenario #%d:\n",++cas);
		if(flag)
			printf("Suspicious bugs found!\n");
		else
			printf("No suspicious bugs found!\n");
		printf("\n");
	}
    return 0;
}

hdu1829 A Bug's Life (并查集)

时间: 2024-10-14 13:28:27

hdu1829 A Bug's Life (并查集)的相关文章

hdu1829 A Bug&#39;s Life(并查集)

开两个并查集,然后合并的时候要合并两次,这样在合并之前判断是否冲突,如果不冲突就进行合并,否则不需要继续合并. #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int MAX=2000; int pre[2*MAX+5]; bool mark; void init(int n){ int i; for(i=1

poj2492--A Bug&#39;s Life(并查集变形)

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

hdu 1829 A Bug&#39;s Life 并查集系列

1 #include "cstdio" 2 #include "iostream" 3 #include "cstring" 4 #include "vector" 5 #include "queue" 6 using namespace std; 7 8 #define MAXN 2222 9 int fa[MAXN]; 10 int rnk[MAXN]; //秩 表示某点与根的距离 11 int n,

POJ 2492 || HDU 1829:A Bug&#39;s Life(并查集)

传送门: POJ:点击打开链接 HDU:点击打开链接 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 27624   Accepted: 8979 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they fe

POJ 2492 A Bug&#39;s Life (并查集)

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

poj2492 A Bug&#39;s Life (并查集拓展)

C - A Bug's Life Crawling in process... Crawling failed Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: CSUST_11 (2013-04-14) System Crawler (2016-05-13) Description B

hdu 1829 A Bug&#39;s Life 并查集

点击打开链接 http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:给出n个关系啊a,b,表示a和b是异性,问是否存在矛盾,即存在a和b是同性. 思路,并查集,用f[i]记录i的父亲,next[i]表示同一个集合里的下一个元素,根据next数组可以遍历出一个集合里的所有元素,len[i]表示i和集合的根的关系,0表示同性,1表示异性. 每输入一个a和b,找到他们的根,如果根相等,那么就判断他们和根的关系,如果和根都是同性关系或者都是异性关系,则表示a

poj 2492A Bug&#39;s Life(并查集)

1 /* 2 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 3 要求统计交配过程中是否出现冲突,即是否有两个同性的昆虫发生交配. 4 5 思路:并查集 6 将同性的昆虫放入集合之中,如果输入的昆虫u, v同时出现在了集合中,那么 u,v就是同性的!发生了同性交配的可能! 7 */ 8 9 #include <string> 10 #include <cst

A Bug&#39;s Life____并查集

English preparation: falsify     伪造:篡改:歪曲:证明...虚假 the sexual behavior of a rare species of bugs. 一种稀有昆虫的性行为. the number of scenarios 场景数量 consecutively 连续地 趣语: 这个时代,是个雷人的时代.凤姐的出现,让我们对自己的审美观产生了怀疑:著姐的降临,使我们对自己的性取向做出了更正.而如今,bug们被同性恋了,这直接就是商人们为了扩大HS产业的炒作

poj2492--A Bug&amp;#39;s Life(并查集变形)

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