aboutsummaryrefslogtreecommitdiff
path: root/rcc-legacy/tests/sysy_lvX/union_find.sy
blob: 98765bb258a22d5b22a0830b3a71720afe315383 (plain)
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
const int N = 1024;
int p[N];

int parent(int n) {
    if (p[n] == n) {
        return n;
    } else {
        p[n] = parent(p[n]);
        return p[n];
    }
}

void init() {
    int i = 0;
    while (i < N) {
        p[i] = i;
        i = i + 1;
    }
}

int main() {
    int n = getint();
    int i = 0;
    int a[N], b[N];
    starttime();
    while (i < n) {
        a[i] = getint();
        b[i] = getint();
        i = i + 1;
    }

    starttime();
    init();
    i = 0;
    while (i < n) {
        p[a[i]] = parent(b[i]);
        i = i + 1;
    }

    i = 0;
    while (i < n) {
        parent(i);
        i = i + 1;
    }
    stoptime();

    i = 0;
    while (i < n) {
        putint(p[i]);
        putch(32);
        i = i + 1;
    }
    return 0;
}