답안 #1073031

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1073031 2024-08-24T08:46:58 Z mc061 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
163 ms 28272 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1001;

struct DSU {
    vector<int> p;
    vector<vector<int>> els;
    DSU() {
        p.resize(N);
        els.resize(N);
        iota(p.begin(), p.end(), 0);
        for (int i = 0; i < N; ++i) {
            els[i].push_back(i);
        }
    }
    int get(int a) {
        return p[a] = (a == p[a] ? a : get(p[a]));
    }
    void merge(int a, int b) {
        int x = get(a);
        int y = get(b);
        if (x == y) return;
        if (els[x].size() > els[y].size()) {
            swap(x, y);
        }
        p[x] = y;
        while (els[x].size()) els[y].push_back(els[x].back()), els[x].pop_back();
    }
};

vector<int> graph[N];
vector<vector<int>> cur_p(N, vector<int>(N, 0));

int cnt[N]={};
vector<int> path;
void dfs(int start, int v) {
    path.push_back(v);
    cur_p[start][v]++;
    cnt[v]++;
    for (int u : graph[v]) {
        if (cnt[u] == 0) dfs(start, u);
    }
    cnt[v]--;
    path.pop_back();
}

void build(vector<vector<int>> b);
int construct(vector<vector<int>> p) {
    DSU lines;
    const int n = p.size();
    vector<vector<int>> b(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            if (p[i][j] == 1) {
                int x = lines.get(i);
                int y = lines.get(j);
                if (x != y) {
                    b[i][j] = b[j][i] = 1;
                    lines.merge(x, y);
                }
            }
        }
    }
    DSU comps;
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            if (p[i][j] > 0) comps.merge(i, j);
        }
    }
    vector<bool> vis(n, false);
    for (int i = 0; i < n; ++i) {
        int j = comps.get(i);
        if (vis[j]) continue;
        vis[j] = true;
        vector<int> vs = comps.els[j];
        vector<int> twos;
        vector<bool> vis_lines(n, false);
        for (int k = 0; k < vs.size(); ++k) {
            int v = vs[k];
            if (vis_lines[lines.get(v)]) continue;
            int u = lines.els[lines.get(v)].front();
            twos.push_back(u);
            vis_lines[lines.get(v)] = true;
        }
        if (twos.size() == 1) continue;
        for (int k = 0; k < twos.size(); ++k) {
            int nxt = (k+1)%twos.size();
            b[twos[k]][twos[nxt]] = b[twos[nxt]][twos[k]] = 1;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            if (b[i][j]) graph[i].push_back(j), graph[j].push_back(i);
        }
    }
    for (int i = 0; i < n; ++i) {
        dfs(i, i);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (cur_p[i][j] != p[i][j]) return 0;
        }
    }
    build(b);
    return true;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:79:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         for (int k = 0; k < vs.size(); ++k) {
      |                         ~~^~~~~~~~~~~
supertrees.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |         for (int k = 0; k < twos.size(); ++k) {
      |                         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 9 ms 5352 KB Output is correct
7 Correct 130 ms 28180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 9 ms 5352 KB Output is correct
7 Correct 130 ms 28180 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 8 ms 5444 KB Output is correct
13 Correct 131 ms 28196 KB Output is correct
14 Correct 3 ms 4444 KB Output is correct
15 Correct 2 ms 4692 KB Output is correct
16 Correct 5 ms 4956 KB Output is correct
17 Correct 82 ms 18196 KB Output is correct
18 Correct 3 ms 4444 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 35 ms 10516 KB Output is correct
21 Correct 122 ms 28244 KB Output is correct
22 Correct 153 ms 28240 KB Output is correct
23 Correct 133 ms 28240 KB Output is correct
24 Correct 124 ms 28244 KB Output is correct
25 Correct 56 ms 18256 KB Output is correct
26 Correct 63 ms 18260 KB Output is correct
27 Correct 135 ms 28192 KB Output is correct
28 Correct 139 ms 28240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4440 KB Output is correct
2 Correct 3 ms 4444 KB Output is correct
3 Correct 2 ms 4464 KB Output is correct
4 Correct 2 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 10 ms 5468 KB Output is correct
9 Correct 124 ms 27988 KB Output is correct
10 Correct 3 ms 4440 KB Output is correct
11 Correct 2 ms 4424 KB Output is correct
12 Correct 8 ms 5452 KB Output is correct
13 Correct 151 ms 28268 KB Output is correct
14 Correct 3 ms 4440 KB Output is correct
15 Correct 3 ms 4444 KB Output is correct
16 Correct 7 ms 4916 KB Output is correct
17 Correct 88 ms 18428 KB Output is correct
18 Correct 3 ms 4440 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 3 ms 4444 KB Output is correct
21 Correct 34 ms 10428 KB Output is correct
22 Correct 161 ms 28240 KB Output is correct
23 Correct 123 ms 28244 KB Output is correct
24 Correct 163 ms 28236 KB Output is correct
25 Correct 57 ms 18192 KB Output is correct
26 Correct 64 ms 18336 KB Output is correct
27 Correct 120 ms 28064 KB Output is correct
28 Correct 154 ms 28240 KB Output is correct
29 Correct 54 ms 18256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4440 KB Output is correct
2 Correct 4 ms 4440 KB Output is correct
3 Correct 2 ms 4444 KB Output is correct
4 Correct 33 ms 10292 KB Output is correct
5 Correct 149 ms 28240 KB Output is correct
6 Correct 117 ms 28212 KB Output is correct
7 Correct 145 ms 28244 KB Output is correct
8 Correct 3 ms 4444 KB Output is correct
9 Correct 42 ms 10496 KB Output is correct
10 Correct 123 ms 28272 KB Output is correct
11 Correct 130 ms 27984 KB Output is correct
12 Correct 142 ms 28240 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 2 ms 4444 KB Output is correct
15 Correct 2 ms 4524 KB Output is correct
16 Correct 32 ms 10268 KB Output is correct
17 Correct 119 ms 28240 KB Output is correct
18 Correct 143 ms 28248 KB Output is correct
19 Correct 121 ms 28072 KB Output is correct
20 Correct 122 ms 28244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 9 ms 5352 KB Output is correct
7 Correct 130 ms 28180 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 8 ms 5444 KB Output is correct
13 Correct 131 ms 28196 KB Output is correct
14 Correct 3 ms 4444 KB Output is correct
15 Correct 2 ms 4692 KB Output is correct
16 Correct 5 ms 4956 KB Output is correct
17 Correct 82 ms 18196 KB Output is correct
18 Correct 3 ms 4444 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 35 ms 10516 KB Output is correct
21 Correct 122 ms 28244 KB Output is correct
22 Correct 153 ms 28240 KB Output is correct
23 Correct 133 ms 28240 KB Output is correct
24 Correct 124 ms 28244 KB Output is correct
25 Correct 56 ms 18256 KB Output is correct
26 Correct 63 ms 18260 KB Output is correct
27 Correct 135 ms 28192 KB Output is correct
28 Correct 139 ms 28240 KB Output is correct
29 Correct 3 ms 4440 KB Output is correct
30 Correct 3 ms 4444 KB Output is correct
31 Correct 2 ms 4464 KB Output is correct
32 Correct 2 ms 4444 KB Output is correct
33 Correct 2 ms 4444 KB Output is correct
34 Correct 2 ms 4444 KB Output is correct
35 Correct 2 ms 4444 KB Output is correct
36 Correct 10 ms 5468 KB Output is correct
37 Correct 124 ms 27988 KB Output is correct
38 Correct 3 ms 4440 KB Output is correct
39 Correct 2 ms 4424 KB Output is correct
40 Correct 8 ms 5452 KB Output is correct
41 Correct 151 ms 28268 KB Output is correct
42 Correct 3 ms 4440 KB Output is correct
43 Correct 3 ms 4444 KB Output is correct
44 Correct 7 ms 4916 KB Output is correct
45 Correct 88 ms 18428 KB Output is correct
46 Correct 3 ms 4440 KB Output is correct
47 Correct 3 ms 4444 KB Output is correct
48 Correct 3 ms 4444 KB Output is correct
49 Correct 34 ms 10428 KB Output is correct
50 Correct 161 ms 28240 KB Output is correct
51 Correct 123 ms 28244 KB Output is correct
52 Correct 163 ms 28236 KB Output is correct
53 Correct 57 ms 18192 KB Output is correct
54 Correct 64 ms 18336 KB Output is correct
55 Correct 120 ms 28064 KB Output is correct
56 Correct 154 ms 28240 KB Output is correct
57 Correct 54 ms 18256 KB Output is correct
58 Correct 2 ms 4444 KB Output is correct
59 Correct 3 ms 4396 KB Output is correct
60 Correct 5 ms 4952 KB Output is correct
61 Correct 80 ms 18216 KB Output is correct
62 Correct 2 ms 4444 KB Output is correct
63 Correct 2 ms 4444 KB Output is correct
64 Correct 2 ms 4424 KB Output is correct
65 Correct 33 ms 10324 KB Output is correct
66 Correct 65 ms 18260 KB Output is correct
67 Correct 57 ms 18340 KB Output is correct
68 Correct 69 ms 18260 KB Output is correct
69 Correct 54 ms 18256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4440 KB Output is correct
2 Correct 2 ms 4444 KB Output is correct
3 Correct 3 ms 4444 KB Output is correct
4 Correct 3 ms 4444 KB Output is correct
5 Correct 3 ms 4444 KB Output is correct
6 Correct 9 ms 5352 KB Output is correct
7 Correct 130 ms 28180 KB Output is correct
8 Correct 2 ms 4440 KB Output is correct
9 Correct 2 ms 4444 KB Output is correct
10 Correct 2 ms 4444 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 8 ms 5444 KB Output is correct
13 Correct 131 ms 28196 KB Output is correct
14 Correct 3 ms 4444 KB Output is correct
15 Correct 2 ms 4692 KB Output is correct
16 Correct 5 ms 4956 KB Output is correct
17 Correct 82 ms 18196 KB Output is correct
18 Correct 3 ms 4444 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 35 ms 10516 KB Output is correct
21 Correct 122 ms 28244 KB Output is correct
22 Correct 153 ms 28240 KB Output is correct
23 Correct 133 ms 28240 KB Output is correct
24 Correct 124 ms 28244 KB Output is correct
25 Correct 56 ms 18256 KB Output is correct
26 Correct 63 ms 18260 KB Output is correct
27 Correct 135 ms 28192 KB Output is correct
28 Correct 139 ms 28240 KB Output is correct
29 Correct 3 ms 4440 KB Output is correct
30 Correct 3 ms 4444 KB Output is correct
31 Correct 2 ms 4464 KB Output is correct
32 Correct 2 ms 4444 KB Output is correct
33 Correct 2 ms 4444 KB Output is correct
34 Correct 2 ms 4444 KB Output is correct
35 Correct 2 ms 4444 KB Output is correct
36 Correct 10 ms 5468 KB Output is correct
37 Correct 124 ms 27988 KB Output is correct
38 Correct 3 ms 4440 KB Output is correct
39 Correct 2 ms 4424 KB Output is correct
40 Correct 8 ms 5452 KB Output is correct
41 Correct 151 ms 28268 KB Output is correct
42 Correct 3 ms 4440 KB Output is correct
43 Correct 3 ms 4444 KB Output is correct
44 Correct 7 ms 4916 KB Output is correct
45 Correct 88 ms 18428 KB Output is correct
46 Correct 3 ms 4440 KB Output is correct
47 Correct 3 ms 4444 KB Output is correct
48 Correct 3 ms 4444 KB Output is correct
49 Correct 34 ms 10428 KB Output is correct
50 Correct 161 ms 28240 KB Output is correct
51 Correct 123 ms 28244 KB Output is correct
52 Correct 163 ms 28236 KB Output is correct
53 Correct 57 ms 18192 KB Output is correct
54 Correct 64 ms 18336 KB Output is correct
55 Correct 120 ms 28064 KB Output is correct
56 Correct 154 ms 28240 KB Output is correct
57 Correct 54 ms 18256 KB Output is correct
58 Correct 3 ms 4440 KB Output is correct
59 Correct 4 ms 4440 KB Output is correct
60 Correct 2 ms 4444 KB Output is correct
61 Correct 33 ms 10292 KB Output is correct
62 Correct 149 ms 28240 KB Output is correct
63 Correct 117 ms 28212 KB Output is correct
64 Correct 145 ms 28244 KB Output is correct
65 Correct 3 ms 4444 KB Output is correct
66 Correct 42 ms 10496 KB Output is correct
67 Correct 123 ms 28272 KB Output is correct
68 Correct 130 ms 27984 KB Output is correct
69 Correct 142 ms 28240 KB Output is correct
70 Correct 2 ms 4444 KB Output is correct
71 Correct 2 ms 4444 KB Output is correct
72 Correct 2 ms 4524 KB Output is correct
73 Correct 32 ms 10268 KB Output is correct
74 Correct 119 ms 28240 KB Output is correct
75 Correct 143 ms 28248 KB Output is correct
76 Correct 121 ms 28072 KB Output is correct
77 Correct 122 ms 28244 KB Output is correct
78 Correct 2 ms 4444 KB Output is correct
79 Correct 3 ms 4396 KB Output is correct
80 Correct 5 ms 4952 KB Output is correct
81 Correct 80 ms 18216 KB Output is correct
82 Correct 2 ms 4444 KB Output is correct
83 Correct 2 ms 4444 KB Output is correct
84 Correct 2 ms 4424 KB Output is correct
85 Correct 33 ms 10324 KB Output is correct
86 Correct 65 ms 18260 KB Output is correct
87 Correct 57 ms 18340 KB Output is correct
88 Correct 69 ms 18260 KB Output is correct
89 Correct 54 ms 18256 KB Output is correct
90 Correct 2 ms 4440 KB Output is correct
91 Correct 2 ms 4444 KB Output is correct
92 Correct 5 ms 4956 KB Output is correct
93 Correct 64 ms 18260 KB Output is correct
94 Correct 2 ms 4440 KB Output is correct
95 Correct 2 ms 4444 KB Output is correct
96 Correct 2 ms 4444 KB Output is correct
97 Correct 15 ms 8028 KB Output is correct
98 Correct 64 ms 18192 KB Output is correct
99 Correct 58 ms 18264 KB Output is correct
100 Correct 61 ms 18192 KB Output is correct
101 Correct 59 ms 18076 KB Output is correct
102 Correct 77 ms 18340 KB Output is correct
103 Correct 86 ms 18240 KB Output is correct
104 Correct 61 ms 18208 KB Output is correct
105 Correct 92 ms 18192 KB Output is correct