Submission #703920

# Submission time Handle Problem Language Result Execution time Memory
703920 2023-02-28T21:35:26 Z bebra Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
197 ms 22456 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
 
#define dbg(x) cerr << #x << ": " << x << endl;
 
 
struct DSU {
    vector<int> parent;
    vector<int> size;
    int comps_n;
 
    DSU(int n) {
        parent.resize(n);
        iota(parent.begin(), parent.end(), 0);
        size.assign(n, 1);
        comps_n = n;
    }
 
    int find(int v) {
        if (parent[v] == v) {
            return v;
        } else {
            return parent[v] = find(parent[v]);
        }
    }
 
    void unite(int u, int v) {
        u = find(u);
        v = find(v);
        if (u == v) return;
        if (size[u] > size[v]) swap(u, v);
        parent[u] = v;
        size[v] += size[u];
    }
};


void dfs(int v, vector<bool>& used, vector<int>& p, const vector<vector<int>>& g) {
    used[v] = true;
    p[v] += 1;
    if (p[v] > 3) {
        return;
    }
    for (auto u : g[v]) {
        if (!used[u]) {
            dfs(u, used, p, g);
        }
    }
    used[v] = false;
}

vector<vector<int>> gen(const vector<vector<int>>& adj) {
    int n = adj.size();
    vector<vector<int>> g(n);
    for (int u = 0; u < n; ++u) {
        for (int v = 0; v < n; ++v) {
            if (adj[u][v]) {
                g[u].push_back(v);
            }
        }
    }
    vector<vector<int>> p(n, vector<int>(n));
    for (int v = 0; v < n; ++v) {
        vector<bool> used(n);
        dfs(v, used, p[v], g);
    }
    return p;
}
 
int construct(vector<vector<int>> p) {
    int n = p.size();
    vector<vector<int>> g(n, vector<int>(n));
 
    bool ok = true;
    DSU dsu(n);
    for (int u = 0; u < n; ++u) {
        for (int v = u + 1; v < n; ++v) {
            if (p[u][v] > 0) {
                dsu.unite(u, v);
            }
        }
    }
 
    auto add_edge = [&](int u, int v) {
        g[u][v] = g[v][u] = 1;
    };
 
    map<int, vector<int>> comps;
    for (int v = 0; v < n; ++v) {
        comps[dsu.find(v)].push_back(v);
    }
    
    for (const auto& [c, comp] : comps) {
        int m = comp.size();
        if (m == 1) continue;
 
        DSU curr_groups(m);
        vector<int> cycle;
        bool has_cycle = false;
        bool has_two = false;
        bool has_three = false;
        for (int i = 0; i < m; ++i) {
            int u = comp[i];
            for (int j = 0; j < m; ++j) {
                if (i == j) continue;
                int v = comp[j];
                if (u == v) continue;
                if (p[u][v] == 0) {
                    ok = false;
                    break;
                }
                if (p[u][v] == 2) {
                    has_two = true;
                    has_cycle = true;
                }
                if (p[u][v] == 3) {
                    has_three = true;
                    has_cycle = true;
                }
                if (p[u][v] == 1) {
                    curr_groups.unite(i, j);
                }
            }
        }
        if (has_two && has_three) {
            ok = false;
            break;
        }
        if (!has_cycle) {
            for (int i = 0; i < m - 1; ++i) {
                add_edge(comp[i], comp[i + 1]);
            }
            continue;
        }
 
        map<int, vector<int>> groupsf;
        for (int i = 0; i < m; ++i) {
            if (curr_groups.size[curr_groups.find(i)] > 1) {
                groupsf[curr_groups.find(i)].push_back(comp[i]);
            } else {
                cycle.push_back(comp[i]);
            }
        }
 
        int groups_n = groupsf.size();
        vector<vector<int>> groups(groups_n);
        {
            int i = 0;
            for (const auto& [f, v] : groupsf) {
                groups[i] = v;
                ++i;
            }
        }
        
        for (int i = 0; i < groups_n; ++i) {
            int b = groups[i].size();
            for (int j = 0; j < b - 1; ++j) {
                add_edge(groups[i][j], groups[i][j + 1]);
            }
            for (auto u : groups[i]) {
                for (auto v : groups[i]) {
                    if (p[u][v] != 1) {
                        ok = false;
                        break;
                    }
                }

                for (int j = i + 1; j < groups_n; ++j) {
                    for (auto v : groups[j]) {
                        if (has_two && p[u][v] != 2) {
                            ok = false;
                            break;
                        }
                        if (has_three && p[u][v] != 3) {
                            ok = false;
                            break;
                        }
                    }
                }
            }
        }
 
        for (int i = 0; i < groups_n; ++i) {
            cycle.push_back(groups[i].back());
        }

        int cycle_size = cycle.size();
 
        if (has_two && cycle_size <= 2) {
            ok = false;
            break;
        }
        if (has_three && cycle_size <= 3) {
            ok = false;
            break;
        }

        for (int i = 0; i < cycle_size - 1; ++i) {
            add_edge(cycle[i], cycle[i + 1]);
        }
        add_edge(cycle[0], cycle.back());
        if (has_three) {
            add_edge(cycle[0], cycle[2]);
        }
    }
 
	if (ok && gen(g) == p) {
        build(g);
        return 1;
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1208 KB Output is correct
7 Correct 197 ms 22456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1208 KB Output is correct
7 Correct 197 ms 22456 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1244 KB Output is correct
13 Correct 179 ms 22384 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 12292 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 45 ms 5972 KB Output is correct
21 Correct 185 ms 22396 KB Output is correct
22 Correct 170 ms 22288 KB Output is correct
23 Correct 194 ms 22240 KB Output is correct
24 Correct 171 ms 22356 KB Output is correct
25 Correct 69 ms 12408 KB Output is correct
26 Correct 64 ms 12364 KB Output is correct
27 Correct 190 ms 22264 KB Output is correct
28 Correct 175 ms 22416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1284 KB Output is correct
9 Correct 172 ms 22448 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1232 KB Output is correct
13 Correct 195 ms 22308 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 80 ms 12348 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 300 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 43 ms 5964 KB Output is correct
22 Correct 178 ms 22272 KB Output is correct
23 Correct 171 ms 22220 KB Output is correct
24 Correct 194 ms 22368 KB Output is correct
25 Correct 64 ms 12492 KB Output is correct
26 Correct 69 ms 12364 KB Output is correct
27 Correct 180 ms 22220 KB Output is correct
28 Correct 195 ms 22324 KB Output is correct
29 Correct 69 ms 12396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 47 ms 5920 KB Output is correct
5 Correct 179 ms 22244 KB Output is correct
6 Correct 169 ms 22236 KB Output is correct
7 Correct 191 ms 22244 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 49 ms 6032 KB Output is correct
10 Correct 195 ms 22380 KB Output is correct
11 Correct 181 ms 22244 KB Output is correct
12 Correct 194 ms 22300 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 47 ms 6004 KB Output is correct
17 Correct 181 ms 22128 KB Output is correct
18 Correct 176 ms 22104 KB Output is correct
19 Correct 169 ms 22108 KB Output is correct
20 Correct 166 ms 22220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1208 KB Output is correct
7 Correct 197 ms 22456 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1244 KB Output is correct
13 Correct 179 ms 22384 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 12292 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 45 ms 5972 KB Output is correct
21 Correct 185 ms 22396 KB Output is correct
22 Correct 170 ms 22288 KB Output is correct
23 Correct 194 ms 22240 KB Output is correct
24 Correct 171 ms 22356 KB Output is correct
25 Correct 69 ms 12408 KB Output is correct
26 Correct 64 ms 12364 KB Output is correct
27 Correct 190 ms 22264 KB Output is correct
28 Correct 175 ms 22416 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 300 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 300 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1284 KB Output is correct
37 Correct 172 ms 22448 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1232 KB Output is correct
41 Correct 195 ms 22308 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 80 ms 12348 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 300 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 43 ms 5964 KB Output is correct
50 Correct 178 ms 22272 KB Output is correct
51 Correct 171 ms 22220 KB Output is correct
52 Correct 194 ms 22368 KB Output is correct
53 Correct 64 ms 12492 KB Output is correct
54 Correct 69 ms 12364 KB Output is correct
55 Correct 180 ms 22220 KB Output is correct
56 Correct 195 ms 22324 KB Output is correct
57 Correct 69 ms 12396 KB Output is correct
58 Correct 0 ms 296 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 4 ms 852 KB Output is correct
61 Correct 77 ms 12408 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 304 KB Output is correct
65 Correct 43 ms 6020 KB Output is correct
66 Correct 68 ms 12180 KB Output is correct
67 Correct 72 ms 12172 KB Output is correct
68 Correct 65 ms 12160 KB Output is correct
69 Correct 68 ms 12296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 9 ms 1208 KB Output is correct
7 Correct 197 ms 22456 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 8 ms 1244 KB Output is correct
13 Correct 179 ms 22384 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 12292 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 45 ms 5972 KB Output is correct
21 Correct 185 ms 22396 KB Output is correct
22 Correct 170 ms 22288 KB Output is correct
23 Correct 194 ms 22240 KB Output is correct
24 Correct 171 ms 22356 KB Output is correct
25 Correct 69 ms 12408 KB Output is correct
26 Correct 64 ms 12364 KB Output is correct
27 Correct 190 ms 22264 KB Output is correct
28 Correct 175 ms 22416 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 300 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 300 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1284 KB Output is correct
37 Correct 172 ms 22448 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1232 KB Output is correct
41 Correct 195 ms 22308 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 80 ms 12348 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 300 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 43 ms 5964 KB Output is correct
50 Correct 178 ms 22272 KB Output is correct
51 Correct 171 ms 22220 KB Output is correct
52 Correct 194 ms 22368 KB Output is correct
53 Correct 64 ms 12492 KB Output is correct
54 Correct 69 ms 12364 KB Output is correct
55 Correct 180 ms 22220 KB Output is correct
56 Correct 195 ms 22324 KB Output is correct
57 Correct 69 ms 12396 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 47 ms 5920 KB Output is correct
62 Correct 179 ms 22244 KB Output is correct
63 Correct 169 ms 22236 KB Output is correct
64 Correct 191 ms 22244 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 49 ms 6032 KB Output is correct
67 Correct 195 ms 22380 KB Output is correct
68 Correct 181 ms 22244 KB Output is correct
69 Correct 194 ms 22300 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 300 KB Output is correct
73 Correct 47 ms 6004 KB Output is correct
74 Correct 181 ms 22128 KB Output is correct
75 Correct 176 ms 22104 KB Output is correct
76 Correct 169 ms 22108 KB Output is correct
77 Correct 166 ms 22220 KB Output is correct
78 Correct 0 ms 296 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 4 ms 852 KB Output is correct
81 Correct 77 ms 12408 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 304 KB Output is correct
85 Correct 43 ms 6020 KB Output is correct
86 Correct 68 ms 12180 KB Output is correct
87 Correct 72 ms 12172 KB Output is correct
88 Correct 65 ms 12160 KB Output is correct
89 Correct 68 ms 12296 KB Output is correct
90 Correct 0 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 4 ms 792 KB Output is correct
93 Correct 73 ms 12164 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 17 ms 3352 KB Output is correct
98 Correct 68 ms 12216 KB Output is correct
99 Correct 67 ms 12288 KB Output is correct
100 Correct 67 ms 12364 KB Output is correct
101 Correct 64 ms 12292 KB Output is correct
102 Correct 109 ms 16332 KB Output is correct
103 Correct 73 ms 14028 KB Output is correct
104 Correct 79 ms 14072 KB Output is correct
105 Correct 78 ms 14068 KB Output is correct