A Bug
Time Limit: 1 Sec Memory Limit: 128 MB
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.

传送门:SHUOJ1020

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

1
2
3
4
5
6
7
8
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

1
2
3
4
5
Scenario #1:
Suspicious bugs found!

Scenario #2:
No suspicious bugs found!

题解

不止一个集合 这道算是一个经典的应用了,有两种方法,一种是多加一个数组,一种是并两次. 我用的是后一种
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.*;  
import java.util.*;
public class Main {
static int[]pre;
static int[]rel;
static boolean flag;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int t=sc.nextInt();
for(int o=1;o<=t;o++){
int n=sc.nextInt();
int m=sc.nextInt();
pre=new int[2*n+2];
rel=new int[2*n+2];
flag=true;
for(int i=1;i<=2*n;i++){
pre[i]=i;
rel[i]=0;
}
for(int i=1;i<=m;i++){
int a=sc.nextInt();
int b=sc.nextInt();
union(a,b+n);
union(a+n,b);
}
pw.println("Scenario #"+o+":");
if(flag) pw.println("No suspicious bugs found!");
else pw.println("Suspicious bugs found!");
if(o!=t)
pw.println();
pw.flush();
}
}
static int find(int x){
int r=pre[x];
while(x!=pre[x]){
x=pre[x];
}
while(r!=x){
int t=pre[r];
pre[r]=x;
r=t;
}
return x;
}
static void union(int a,int b){
int fa=find(a);
int fb=find(b);
if(fa==fb) flag=false;
else{
pre[fa]=fb;
}
}
}