A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13928 Accepted Submission(s): 4528
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.
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!
题目大意:每次输入的两只虫子互为异性 判断之后的输入中是否会出现矛盾的情况
例如:1 2
2 3
1 3
很明显3双性了很尴尬。。。这时就输出“Suspicious bugs found!”。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;
int pre[10000], s[10000];
int find(int x)
{
if (pre[x] == x)
return x;
else
return pre[x] = find(pre[x]);
}
void join(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fx != fy)
pre[fx] = fy;
}
int main()
{
std::cin.tie(false);
ios::sync_with_stdio(false);
int t, n, m, a, b, Case = 0;
cin >> t;
for (int i = 0; i < t; i++)
{
Case++;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
pre[i] = i;
s[i] = 0;
}
int flag = 0;
while (m--)
{
cin >> a >> b;
if (flag)
continue;
if (find(a) == find(b))
{
flag = 1;
continue;
}
if (s[a] == 0) s[a] = b;
else join(s[a], b);
if (s[b] == 0) s[b] = a;
else join(s[b], a);
}
printf("Scenario #%d:\n", Case);
if (flag) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
printf("\n");
}
system("pause");
}