首页 > 代码库 > poj1988

poj1988

?
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
#include<iostream>
using namespace std;
 
const int maxn = 30000+100;
 
int F[maxn], D[maxn], S[maxn];
 
void set(int n)
{
    for(int i =0 ;i < n; i++)
    {
        F[i] = i;
        D[i] = 0;
        S[i] = 1;
    }
}
void find(int x)
{
    if(x != F[x])
    {
        find(F[x]);
        D[x]+= D[F[x]];
        F[x] = F[F[x]];
    }
}
 
void Union(int x, int y)
{
    find(x);find(y);
    x = F[x]; y= F[y];
    F[x] = y;
    D[x] = S[y];
    S[y] += S[x];
}
int main()
{
    set(maxn);
    int P;
    cin>>P;
    char c; int a, b;
    while(P--)
    {
        cin >> c;
        if(c==‘M‘)
        {
            cin>> a >> b;
            Union(a, b);
        }else
        {
            cin>>a;
            find(a);
            cout << D[a] <<endl;
        }
    }
    return 0;
}