답안 #1057143

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1057143 2024-08-13T14:31:22 Z arbuzick 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
153 ms 24188 KB
#include "supertrees.h"

#include <bits/stdc++.h>

using namespace std;

struct DSU {
    int n;
    vector<int> sz, p;

    DSU(int _n) {
        n = _n;
        sz.assign(n, 1);
        p.resize(n);
        iota(p.begin(), p.end(), 0);
    }

    int pr(int v) {
        if (p[v] == v) {
            return v;
        }
        return p[v] = pr(p[v]);
    }

    void unite(int a, int b) {
        a = pr(a), b = pr(b);
        if (a == b) {
            return;
        }
        if (sz[a] < sz[b]) {
            swap(a, b);
        }
        p[b] = a;
        sz[a] += sz[b];
    }
};

int construct(vector<vector<int>> p) {
    int n = p.size();
    vector<vector<int>> answer(n, vector<int>(n));
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 3) {
                return 0;
            }
        }
    }

    DSU d0(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] != 0) {
                d0.unite(i, j);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (d0.pr(i) == d0.pr(j) && p[i][j] == 0) {
                return 0;
            }
        }
    }

    DSU d1(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 1) {
                d1.unite(i, j);
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (d1.pr(i) == d1.pr(j) && p[i][j] != 1) {
                return 0;
            }
        }
        if (d1.pr(i) != i) {
            answer[d1.pr(i)][i] = answer[i][d1.pr(i)] = 1;
        }
    }

    DSU d2(n);
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (p[i][j] == 2) {
                d2.unite(i, j);
            }
        }
    }
    vector<vector<int>> cycles(n);
    for (int i = 0; i < n; ++i) {
        if (d2.sz[d2.pr(i)] != 1 && i == d1.pr(i)) {
            cycles[d2.pr(i)].push_back(i);
        }
    }
    for (int i = 0; i < n; ++i) {
        if (cycles[i].size() == 2) {
            return 0;
        }
        for (int j = 0; j < (int)cycles[i].size(); ++j) {
            int nxt = j + 1;
            if (nxt == (int)cycles[i].size()) {
                nxt = 0;
            }
            answer[cycles[i][j]][cycles[i][nxt]] = answer[cycles[i][nxt]][cycles[i][j]] = 1;
        }
    }

    build(answer);
    return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 103 ms 24052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 103 ms 24052 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 436 KB Output is correct
12 Correct 4 ms 1400 KB Output is correct
13 Correct 102 ms 24076 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 2 ms 936 KB Output is correct
17 Correct 50 ms 14216 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 28 ms 6484 KB Output is correct
21 Correct 100 ms 24056 KB Output is correct
22 Correct 98 ms 23940 KB Output is correct
23 Correct 119 ms 24148 KB Output is correct
24 Correct 97 ms 24144 KB Output is correct
25 Correct 42 ms 14088 KB Output is correct
26 Correct 46 ms 14160 KB Output is correct
27 Correct 106 ms 24164 KB Output is correct
28 Correct 97 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 436 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 4 ms 1188 KB Output is correct
9 Correct 97 ms 23888 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 4 ms 1372 KB Output is correct
13 Correct 112 ms 24148 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 49 ms 13996 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 25 ms 6276 KB Output is correct
22 Correct 101 ms 24148 KB Output is correct
23 Correct 102 ms 24160 KB Output is correct
24 Correct 107 ms 24144 KB Output is correct
25 Correct 44 ms 14072 KB Output is correct
26 Correct 49 ms 14160 KB Output is correct
27 Correct 126 ms 24144 KB Output is correct
28 Correct 110 ms 24148 KB Output is correct
29 Correct 42 ms 14164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 26 ms 6216 KB Output is correct
5 Correct 105 ms 23944 KB Output is correct
6 Correct 103 ms 24144 KB Output is correct
7 Correct 153 ms 24184 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 27 ms 6356 KB Output is correct
10 Correct 99 ms 24188 KB Output is correct
11 Correct 116 ms 24044 KB Output is correct
12 Correct 104 ms 24072 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 33 ms 6272 KB Output is correct
17 Correct 100 ms 23976 KB Output is correct
18 Correct 116 ms 23772 KB Output is correct
19 Correct 99 ms 23888 KB Output is correct
20 Correct 104 ms 24068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 103 ms 24052 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 436 KB Output is correct
12 Correct 4 ms 1400 KB Output is correct
13 Correct 102 ms 24076 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 2 ms 936 KB Output is correct
17 Correct 50 ms 14216 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 28 ms 6484 KB Output is correct
21 Correct 100 ms 24056 KB Output is correct
22 Correct 98 ms 23940 KB Output is correct
23 Correct 119 ms 24148 KB Output is correct
24 Correct 97 ms 24144 KB Output is correct
25 Correct 42 ms 14088 KB Output is correct
26 Correct 46 ms 14160 KB Output is correct
27 Correct 106 ms 24164 KB Output is correct
28 Correct 97 ms 24056 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 436 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 4 ms 1188 KB Output is correct
37 Correct 97 ms 23888 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 4 ms 1372 KB Output is correct
41 Correct 112 ms 24148 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 49 ms 13996 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 25 ms 6276 KB Output is correct
50 Correct 101 ms 24148 KB Output is correct
51 Correct 102 ms 24160 KB Output is correct
52 Correct 107 ms 24144 KB Output is correct
53 Correct 44 ms 14072 KB Output is correct
54 Correct 49 ms 14160 KB Output is correct
55 Correct 126 ms 24144 KB Output is correct
56 Correct 110 ms 24148 KB Output is correct
57 Correct 42 ms 14164 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 2 ms 960 KB Output is correct
61 Correct 52 ms 14220 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 25 ms 6248 KB Output is correct
66 Correct 43 ms 13836 KB Output is correct
67 Correct 42 ms 13816 KB Output is correct
68 Correct 46 ms 13908 KB Output is correct
69 Correct 42 ms 13908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 4 ms 1372 KB Output is correct
7 Correct 103 ms 24052 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 436 KB Output is correct
12 Correct 4 ms 1400 KB Output is correct
13 Correct 102 ms 24076 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 2 ms 936 KB Output is correct
17 Correct 50 ms 14216 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 28 ms 6484 KB Output is correct
21 Correct 100 ms 24056 KB Output is correct
22 Correct 98 ms 23940 KB Output is correct
23 Correct 119 ms 24148 KB Output is correct
24 Correct 97 ms 24144 KB Output is correct
25 Correct 42 ms 14088 KB Output is correct
26 Correct 46 ms 14160 KB Output is correct
27 Correct 106 ms 24164 KB Output is correct
28 Correct 97 ms 24056 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 0 ms 436 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 4 ms 1188 KB Output is correct
37 Correct 97 ms 23888 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 4 ms 1372 KB Output is correct
41 Correct 112 ms 24148 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 2 ms 860 KB Output is correct
45 Correct 49 ms 13996 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 25 ms 6276 KB Output is correct
50 Correct 101 ms 24148 KB Output is correct
51 Correct 102 ms 24160 KB Output is correct
52 Correct 107 ms 24144 KB Output is correct
53 Correct 44 ms 14072 KB Output is correct
54 Correct 49 ms 14160 KB Output is correct
55 Correct 126 ms 24144 KB Output is correct
56 Correct 110 ms 24148 KB Output is correct
57 Correct 42 ms 14164 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 0 ms 344 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 26 ms 6216 KB Output is correct
62 Correct 105 ms 23944 KB Output is correct
63 Correct 103 ms 24144 KB Output is correct
64 Correct 153 ms 24184 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 27 ms 6356 KB Output is correct
67 Correct 99 ms 24188 KB Output is correct
68 Correct 116 ms 24044 KB Output is correct
69 Correct 104 ms 24072 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 33 ms 6272 KB Output is correct
74 Correct 100 ms 23976 KB Output is correct
75 Correct 116 ms 23772 KB Output is correct
76 Correct 99 ms 23888 KB Output is correct
77 Correct 104 ms 24068 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 2 ms 960 KB Output is correct
81 Correct 52 ms 14220 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 25 ms 6248 KB Output is correct
86 Correct 43 ms 13836 KB Output is correct
87 Correct 42 ms 13816 KB Output is correct
88 Correct 46 ms 13908 KB Output is correct
89 Correct 42 ms 13908 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 2 ms 860 KB Output is correct
93 Correct 42 ms 13856 KB Output is correct
94 Correct 0 ms 604 KB Output is correct
95 Correct 0 ms 432 KB Output is correct
96 Correct 0 ms 348 KB Output is correct
97 Correct 10 ms 3844 KB Output is correct
98 Correct 48 ms 14052 KB Output is correct
99 Correct 42 ms 13916 KB Output is correct
100 Correct 42 ms 13952 KB Output is correct
101 Correct 48 ms 13904 KB Output is correct
102 Correct 41 ms 13908 KB Output is correct
103 Correct 47 ms 13956 KB Output is correct
104 Correct 42 ms 14092 KB Output is correct
105 Correct 46 ms 13908 KB Output is correct