pku 1611
2009年7月12日 星期日
題目鏈接:PKU 1611 The Suspects
分類:最基礎的并查集
Code:
1
#include<iostream>
2
using namespace std;
3
#define MAXN 30000
4
int parent[MAXN];
5
6
void init(int n=MAXN)
7

{
8
int i;
9
for(i=0;i<n;i++)parent[i]=-1;
10
}
11
int find(int x)
12

{
13
if(parent[x]<0) return x;
14
else return parent[x]=find(parent[x]);
15
}
16
17
int Union(int x,int y)
18

{
19
int root1=find(x),root2=find(y);
20
if(root1==root2) return root1;
21
if(parent[root1]<parent[root2])
22
{
23
parent[root1]+=parent[root2];
24
parent[root2]=root1;
25
return root1;
26
}
27
else
28
{
29
parent[root2]+=parent[root1];
30
parent[root1]=root2;
31
return root2;
32
}
33
}
34
35
int main()
36

{
37
int n,m,root,i,k,j,node;
38
while(scanf("%d%d",&n,&m)!=EOF)
39
{
40
if(!n&&!m)break;
41
init(n);
42
for(i=0;i<m;i++)
43
{
44
scanf("%d",&k);
45
for(j=0;j<k;j++)
46
{
47
scanf("%d",&node);
48
if(j==0)root=node;
49
root=Union(root,node);
50
}
51
}
52
printf("%d\n",-parent[find(0)]);
53
}
54
return 0;
55
}
56
57
題目鏈接:PKU 1611 The Suspects
分類:最基礎的并查集
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

56

57
