(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;
}





               


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值