Swust OJ 003 A Bug(判断图中是否有环)

Time limit(ms): 1000
Memory limit(kb): 65535
Submission: 406
Accepted: 139
Accepted
test  判断图是否有环  并查集  bug

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.


 

Description

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.

Input

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.

Output
1
2
3
4
5
6
7
8
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Input

1
2
3
4
5
Scenario #1:
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
/*
 *  给臭虫m组序号a,b 则a,b两个相恋 默认a,b异性  问是否存在同性恋  比如 ab bc ac 那么  必存在同性恋 因为假设b为公 则a,c为母 而ac也是
    相恋的
	故本题实质上是判断途中是否有环  有环必有同性恋...
	怎么判断图中有环  这个博客写的挺好的 http://www.cnblogs.com/xwdreamer/archive/2011/06/11/2297008.html
 *  你问我为什么这个Swust OJ这么多毛病 还要用  母校情怀啊。。。
*/

/* dfs版本加邻接矩阵版本 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <cmath>
#include <queue>
using namespace std;
int map[1001][1001];
bool vis[1001][1001];
int n, m;
int cnt = 0;
bool ans = false;
void dfs(int a,int b) {
	if (a == b) {                      
		cnt++;
		if (cnt == 2) {
			ans = 1;
			return;
		}
	}
	for (int i = 1; i <= n; i++) {
		if (a != i && !vis[a][i] && !vis[i][a] && map[a][i]==1)  {
			vis[a][i] = vis[i][a] = 1;
			dfs(i, b);
			vis[a][i] = vis[i][a] = 0;
		}
	}
}
int main(){
	int t;
	cin >> t;
	for (int i = 1; i <= t; i++) {
		printf("Scenario #%d:\n",i);
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				map[i][j] = 0;
		for (int j = 0; j < m; j++) {
			int a, b;
			cin >> a >> b;
			map[a][b] = 1;
			map[b][a] = 1;
		}
		bool flag = true;
		for (int k = 1; k <= n; k++) {
			cnt = 0;
			ans = false;
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++)
					vis[i][j] = 0;
			}
			dfs(k, k);
				if (ans) {
					cout << "Suspicious bugs found!" << endl ;
					flag = false;
					break;
				}
		}
		if(flag)
		cout << "No suspicious bugs found!" << endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值