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.
a到b表示a喜欢b,然后问你这些虫子里有没有同性恋(只有雌雄两个)。
食物链精简版。就是看看他在不在一个树上,在的话,看看和祖先是什么关系,不在就合并。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int n, m;
bool r[2005];
int p[2005];
void init()
{
for (int i = 1; i <= n; i++)
{
p[i] = i; r[i] = 0;
}
}
int find(int x)
{
if (x != p[x])
{
int fx = find(p[x]);
if (r[x] == r[p[x]])r[x] = 0;
else r[x] = 1;
p[x] = fx;
}
return p[x];
}
void union_set(int x, int y)
{
int fx = find(x);
int fy = find(y);
p[fx] = fy;
if (r[x] == r[y])r[fx] = 1;
else r[fx] = 0;
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\巍巍\\Desktop\\in.txt", "r", stdin);
//freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
int t, flag, kase = 1;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
init();
flag = 0;
while (m--)
{
int x, y;
scanf("%d%d", &x, &y);
int fx = find(x);
int fy = find(y);
if (fx == fy&&r[x] == r[y])flag = 1;
else if (fx != fy)
{
union_set(x, y);
}
}
printf("Scenario #%d:\n", kase++);
if (flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return 0;
}