(hdu step 5.1.1)A Bug's Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

题目:

A Bug‘s Life

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

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

题目大意:

假设有这个两组数据

1  2

1  3

name这时候得到的信息有以下三点:

1)1和2可以配对

2)1和3可以配对

3)2和3属于同性,不能进行配对。也就是说不允许出现“2 3”这种数据

题目分析:

并查集。简单题。通过上面的“题目大意”的理解。其实我们可以分析出美都区一堆数据" a  b "的时候,

我们需要做以下操作:

1)判断a和b的根节点是否相同。如果相同,则不符合题意(因为a b 表示的就是a与b的根节点不同)

2)判断a是否已经由异性群.如果没有,则为a建立异性群,即sex[a]=b。否则将b加入到a的异性群中。

代码如下:

/*
 * a.cpp
 *
 *  Created on: 2015年2月26日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 2001;

int father[maxn];//用于存储父子关系.例如father[i]=i表示i的父亲结点是他自己
int sex[maxn];//用于存储配对关系.sex[a]=b表示a可以与吧配对

/**
 * 寻找a所在树的根节点
 */
int find(int a){
	if(a == father[a]){//如果一个结点的父亲节点是他自己
		return a;//则表示已经找到了根节点
	}

	return father[a] = find(father[a]);//否则继续寻找。
}

/**
 * 合并a、b所在的两棵树
 */
void join(int a,int b){
	int fa = find(a);//找到a所在树的根节点
	int fb = find(b);

	if(fa != fb){//如果a所在树的根节点和b所在树的根节点不相同,则证明a、b分别在两颗不同的树中
		father[fa] = fb;//合并a、b所在的两棵树
	}
}

/**
 * 初始化
 */
void init(){
	int i;
	for(i = 1 ; i < maxn ; ++i){//遍历所有节点
		father[i] = i;//把每个节点的父亲节点都指向他自己
	}
}

int main(){
	int t;
	scanf("%d",&t);
	int k;
	for(k = 1 ; k <= t ; ++k){
		int n;
		int m;

		init();
		memset(sex,0,sizeof(sex));//初始化配对关系.默认谁也没有配对关系

		scanf("%d%d",&n,&m);
		int i;
		bool flag = true;//初始化是否有bug的标记.默认没有bug
		for(i = 1 ; i <= m ; ++i){
			int a,b;
			scanf("%d%d",&a,&b);
			if(flag == true){//如果目前还没有bug,则继续建立新的关系
				int fa = find(a);//找到a的根节点
				int fb = find(b);//找到b的根节点

				if(fa == fb){//如果a与b在同一堆
					flag = false;//不符合题意.将flag标记为false,带白哦有bug
					continue;
				}
				//如果能执行以下代码,则代表a与b的关系正确

				if(sex[a] == 0){//如果a还没有异性树
					sex[a] = b;//给他建立一个异性群.目前这个群只有一个节点b
				}else{//如果a已经有异性群
					join(sex[a],b);//将b加入到异性群中
				}

				//对b执行同样的操作
				if(sex[b] == 0){
					sex[b] = a;
				}else{
					join(sex[b],a);
				}
			}
		}

		printf("Scenario #%d:\n",k);
		if(flag == true){
			printf("No suspicious bugs found!\n");
		}else{
			printf("Suspicious bugs found!\n");
		}

//		if(k != t){//这种写法居然还会PE。需要注意一下
			printf("\n");
//		}
	}

	return 0;
}

(hdu step 5.1.1)A Bug's Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

时间: 2024-10-09 07:44:29

(hdu step 5.1.1)A Bug's Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)的相关文章

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

(hdu step 3.1.5)Tiling_easy version(求用2*1、2*2两种骨格铺满2*n的网格的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 515 Accepted Submission(s): 447   Prob

对非正确使用浮点型数据而导致项目BUG的问题探讨

乘法分配律 在上小学的时候就已经学习过乘法分配律,乘法分配律的具体内容是:两个数的和与一个数相乘,可以先把他们分别与这个数相乘,再相加,得数不变.乘法分配律的定义还可以用表达式"(a+b)×c = a×c+b×c"的形式给出.乘法分配律的反用"a×c+b×c = (a+b)×c"同样成立.例如"10.2×(3+7) = 10.2×3+10.2×7 = 102"(反用形式为"10.2×3+10.2×7 = 10.2×(3+7) = 102

Oracle字符乱码、数据越界访问典型Bug分析

前言: 作为乙方,在甲方客户那里验收阶段发现两个诡异Bug.以下就问题来源.问题根因.解决方案.如何避免做详细描述. 一.Bug1:Oracle读写字符乱码. 1.问题来源 Oracle数据库监听http://blog.csdn.net/laoyang360/article/details/46524519 需要获取最新插入的中文类别字符,以判定分类.单步调试发现每次接收到的都是乱码. 2.问题根因 编码格式不一致导致. 3.解决方案 第一步:查看oracle自身的编码格式,可以通过命令sele

IE中a标签绝对定位时才生的bug

对a链接的display设置为block,以便整个标签可以点击. 但是,如果对该标签设置为position:absolute后,会发现在ie6.ie7下有时点击无效,ie8下有效(ie8标准),使用zoom:1方式也无法解决问题. 解决方案 1.使用position:relative而不是position:absolute: 2.添加背景色: 3.添加透明的背景图片,gif或png,但会增加无意义的http请求: 4.使用background:url(about:blank),最佳方案(推荐):

修复duilib CEditUI控件和CWebBrowserUI控件中按Tab键无法切换焦点的bug

转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41556615 在duilib中,按tab键会让焦点在Button一类的控件中切换,但是切换焦点一直存在bug,具体的描述如下: 1.在主窗体里弹出新的窗体,当新窗体中存在CEditUI控件并且焦点在此CEditUI控件上,那么按tab键将无法切换焦点而一直处于CEditUI中.(只在新窗体中有此bug,主创体中没有,原因会在后面分析) 2.CWebBrowserUI控件