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.
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.
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.
1
2
3
4
5
6
7
8
|
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
|
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;
}