1
#include<iostream>
2
#define MaxN 30000
3
typedef struct node{
4
int count;
5
int parent;
6
int d;
7
}NODE;
8
NODE S[MaxN+1];
9
void init()
10
{
11
for(int i=1;i<=MaxN;i++){
12
S[i].count=1;
13
S[i].d=0;
14
S[i].parent=i;}
15
}
16
int find_set(int x)
17
{
18
int t=S[x].parent;
19
if(S[x].parent!=x){
20
S[x].parent=find_set(S[x].parent);
21
S[x].d+=S[t].d;
22
}
23
return S[x].parent;
24
}
25
void union_set(int x,int y)
26
{
27
x=find_set(x);
28
y=find_set(y);
29
S[y].parent=x;
30
S[y].d=S[x].count;
31
S[x].count+=S[y].count;
32
}
33
int main()
34
{
35
int P,x,y;
36
char c;
37
init();
38
scanf("%d",&P);
39
getchar();
40
while(P>0){
41
P--;
42
c=getchar();
43
if(c=='M'){
44
scanf("%d%d",&x,&y);
45
union_set(x,y);
46
}
47
else {
48
scanf("%d",&x);
49
y=find_set(x);
50
printf("%d\n",S[y].count-S[x].d-1);
51
}
52
getchar();
53
}
54
return 0;
55
}
http://acm.pku.edu.cn/JudgeOnline/problem?id=1988

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
