poj2492 A Bug'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

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.

说起来并查集真是一个好东西

就 我目前知道的而言 1.判断是否成环2.计算环的个数3.计算集合的个数  其实都是一个道理 就是看你怎么想

当然了 并查集还有按秩合并  压缩路径

怎么解释按秩合并呢  其实说白了就是按照两个人的rank等级建立关系

int x=find(a);
  int y=find(b);
  if(rank[x]>rank[y])//按秩合并
    fa[y]=x;
  else
  {
    fa[x]=y;
    if(rank[x]==rank[y])
      rank[y]++;
  }

压缩路径(当看到数组特别大时  慎重啊  爆栈  如hdu1272)

压缩路径的意思就是在查找的过程中 使当前节点的值更新,直到根节点   便于下次查找节省时间

int find(int x)
{
	if(fa[x]!=x) fa[x]=find(fa[x]);
	return fa[x];
}

不压缩路径:

int find(int x)
13.{
14.    while(fa[x]!=x)
15.    x=fa[x];
16.    return fa[x];
17.}

这道题起初真的没理解啊。。直接并查集 结果错了  听到队友说好像要分男女  也想了一个方法 就是判断

这对关系中某个人有没有出现过 一个出现另外一个就好判断了  后来想了一组测试数组 1 2 3 4 1 3  。。。我想错了

最后看的别人的代码  理解了

思路: 我们可以把男 女分为两个集合  如果出现的新的关系里面并查集出现了环  那么 就说明这两个人都是男 或者都是女

#include <stdio.h>
#include <string.h>
int fa[2005];
int rela[2005];
bool bugs;
void init(int n)
{
	for(int i=1;i<=n;i++)
	fa[i]=i,rela[i]=0;
}
int find(int x)
{
	if(fa[x]!=x) fa[x]=find(fa[x]);
	return fa[x];
}
void uni(int a,int b)
{
	int aa=find(a);
	int bb=find(b);
	if(aa!=bb)
	fa[aa]=bb;
}
int main()
{
	int ncase;
	int t=1;
	scanf("%d",&ncase);
	while(ncase--)
	{
		int n,m;
		scanf("%d %d",&n,&m);
		init(n);
		bugs=false;
		for(int i=0;i<m;i++)
		{
			int x,y;
			scanf("%d %d",&x,&y);
			if(!bugs)
			{
				int xx=find(x);
				int yy=find(y);
				if(xx==yy) bugs=true;
				if(rela[x]) uni(rela[x],y);
				else rela[x]=y;
				if(rela[y]) uni(rela[y],x);
				else rela[y]=x;
			}
		}
		printf("Scenario #%d:\n",t++);
		if(bugs)
		puts("Suspicious bugs found!\n");
		else
		puts("No suspicious bugs found!\n");
	}
	return 0;
} 

poj2492 A Bug's Life (并查集拓展)

时间: 2024-10-13 12:23:11

poj2492 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

poj 2492 A Bug&#39;s Life 【并查集拓展】

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 29030   Accepted: 9455 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 并查集

点击打开链接 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产业的炒作