Submission #418053

# Submission time Handle Problem Language Result Execution time Memory
418053 2021-06-05T01:09:20 Z fishy15 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
296 ms 24132 KB
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <numeric>

#include "supertrees.h"

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 1000000

using namespace std;

struct DSU {
    vector<int> dsu;
    vector<int> sz;

    DSU(int n) {
        dsu.resize(n);
        sz.resize(n);
        iota(dsu.begin(), dsu.end(), 0);
        fill(sz.begin(), sz.end(), 1);
    }

    int find(int x) {
        return x == dsu[x] ? x : dsu[x] = find(dsu[x]);
    }

    void join(int x, int y) {
        if ((x = find(x)) != (y = find(y))) {
            if (sz[x] < sz[y]) swap(x, y);
            dsu[y] = x;
            sz[x] += sz[y];
        }
    }
};

bool solve_comp(const vector<vector<int>> &p, const vector<int> &comp, vector<pair<int, int>> &edges) {
    int n = p.size();
    DSU one_same(n); 

    for (int i : comp) {
        for (int j : comp) {
            if (p[i][j] == 0) {
                return false;
            } else if (p[i][j] == 1) {
                one_same.join(i, j);
            }
        }
    }

    vector<vector<int>> one_comp(n);
    for (int i : comp) {
        one_comp[one_same.find(i)].push_back(i);
    }

    // check if each thing in the "line" is reachable from one another
    int first_c = -1;
    int prev_c = -1;
    int cnt = 0;
    for (auto &v : one_comp) {
        if (v.empty()) continue;

        cnt++;
        for (int i : v) {
            for (int j : v) {
                if (p[i][j] != 1) {
                    return false;
                }
            }
        }

        int prev = -1;
        for (int i : v) {
            if (prev != -1) {
                edges.push_back({i, prev});
            }
            prev = i;
        }

        if (prev_c != -1) {
            edges.push_back({v[0], prev_c});
        }

        if (cnt == 1) first_c = v[0];
        prev_c = v[0];
    }

    if (cnt == 2) {
        return false;
    } 

    if (cnt >= 3) {
        edges.push_back({first_c, prev_c});
    }

    return true;
}

int construct(vector<vector<int>> p) {
    int n = p.size();

    DSU same_comp(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 3) {
                return 0;
            } else if (p[i][j] > 0) {
                same_comp.join(i, j);
            }
        }
    }

    vector<vector<int>> comp(n);
    for (int i = 0; i < n; i++) {
        comp[same_comp.find(i)].push_back(i);
    }

    vector<pair<int, int>> edge;
    bool ok = true;
    for (auto &v : comp) {
        if (!v.empty()) {
            bool res = solve_comp(p, v, edge);
            if (!res) {
                ok = false;
                break;
            }
        }
    }

    if (ok) {
        vector<vector<int>> ans(n, vector<int>(n));
        for (auto [x, y] : edge) {
            ans[x][y] = 1;
            ans[y][x] = 1;
        }
        build(ans);
        return 1;
    } else {
        return 0;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 1196 KB Output is correct
7 Correct 273 ms 23940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 1196 KB Output is correct
7 Correct 273 ms 23940 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1272 KB Output is correct
13 Correct 271 ms 24044 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 116 ms 10144 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 58 ms 6152 KB Output is correct
21 Correct 249 ms 24040 KB Output is correct
22 Correct 296 ms 24040 KB Output is correct
23 Correct 247 ms 24132 KB Output is correct
24 Correct 232 ms 24052 KB Output is correct
25 Correct 99 ms 10092 KB Output is correct
26 Correct 95 ms 10104 KB Output is correct
27 Correct 247 ms 24048 KB Output is correct
28 Correct 234 ms 24004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 10 ms 1228 KB Output is correct
9 Correct 238 ms 24060 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 292 KB Output is correct
12 Correct 13 ms 1228 KB Output is correct
13 Correct 236 ms 24048 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 7 ms 588 KB Output is correct
17 Correct 132 ms 10104 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 57 ms 6256 KB Output is correct
22 Correct 232 ms 23996 KB Output is correct
23 Correct 225 ms 24004 KB Output is correct
24 Correct 245 ms 23940 KB Output is correct
25 Correct 96 ms 10208 KB Output is correct
26 Correct 111 ms 10092 KB Output is correct
27 Correct 234 ms 23932 KB Output is correct
28 Correct 264 ms 24004 KB Output is correct
29 Correct 109 ms 10308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 59 ms 6248 KB Output is correct
5 Correct 229 ms 24004 KB Output is correct
6 Correct 231 ms 23972 KB Output is correct
7 Correct 269 ms 23928 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 59 ms 6152 KB Output is correct
10 Correct 234 ms 23924 KB Output is correct
11 Correct 238 ms 24040 KB Output is correct
12 Correct 240 ms 24044 KB Output is correct
13 Correct 1 ms 292 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 65 ms 6256 KB Output is correct
17 Correct 229 ms 24068 KB Output is correct
18 Correct 235 ms 24124 KB Output is correct
19 Correct 226 ms 23936 KB Output is correct
20 Correct 232 ms 23968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 1196 KB Output is correct
7 Correct 273 ms 23940 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1272 KB Output is correct
13 Correct 271 ms 24044 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 116 ms 10144 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 58 ms 6152 KB Output is correct
21 Correct 249 ms 24040 KB Output is correct
22 Correct 296 ms 24040 KB Output is correct
23 Correct 247 ms 24132 KB Output is correct
24 Correct 232 ms 24052 KB Output is correct
25 Correct 99 ms 10092 KB Output is correct
26 Correct 95 ms 10104 KB Output is correct
27 Correct 247 ms 24048 KB Output is correct
28 Correct 234 ms 24004 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 1 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 10 ms 1228 KB Output is correct
37 Correct 238 ms 24060 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 292 KB Output is correct
40 Correct 13 ms 1228 KB Output is correct
41 Correct 236 ms 24048 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 7 ms 588 KB Output is correct
45 Correct 132 ms 10104 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 57 ms 6256 KB Output is correct
50 Correct 232 ms 23996 KB Output is correct
51 Correct 225 ms 24004 KB Output is correct
52 Correct 245 ms 23940 KB Output is correct
53 Correct 96 ms 10208 KB Output is correct
54 Correct 111 ms 10092 KB Output is correct
55 Correct 234 ms 23932 KB Output is correct
56 Correct 264 ms 24004 KB Output is correct
57 Correct 109 ms 10308 KB Output is correct
58 Correct 0 ms 288 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 6 ms 656 KB Output is correct
61 Correct 108 ms 10088 KB Output is correct
62 Correct 1 ms 300 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 58 ms 6252 KB Output is correct
66 Correct 96 ms 10092 KB Output is correct
67 Correct 97 ms 10100 KB Output is correct
68 Correct 99 ms 10088 KB Output is correct
69 Correct 93 ms 10104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 288 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 12 ms 1196 KB Output is correct
7 Correct 273 ms 23940 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1272 KB Output is correct
13 Correct 271 ms 24044 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 588 KB Output is correct
17 Correct 116 ms 10144 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 58 ms 6152 KB Output is correct
21 Correct 249 ms 24040 KB Output is correct
22 Correct 296 ms 24040 KB Output is correct
23 Correct 247 ms 24132 KB Output is correct
24 Correct 232 ms 24052 KB Output is correct
25 Correct 99 ms 10092 KB Output is correct
26 Correct 95 ms 10104 KB Output is correct
27 Correct 247 ms 24048 KB Output is correct
28 Correct 234 ms 24004 KB Output is correct
29 Correct 1 ms 292 KB Output is correct
30 Correct 1 ms 292 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 10 ms 1228 KB Output is correct
37 Correct 238 ms 24060 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 292 KB Output is correct
40 Correct 13 ms 1228 KB Output is correct
41 Correct 236 ms 24048 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 7 ms 588 KB Output is correct
45 Correct 132 ms 10104 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 57 ms 6256 KB Output is correct
50 Correct 232 ms 23996 KB Output is correct
51 Correct 225 ms 24004 KB Output is correct
52 Correct 245 ms 23940 KB Output is correct
53 Correct 96 ms 10208 KB Output is correct
54 Correct 111 ms 10092 KB Output is correct
55 Correct 234 ms 23932 KB Output is correct
56 Correct 264 ms 24004 KB Output is correct
57 Correct 109 ms 10308 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 59 ms 6248 KB Output is correct
62 Correct 229 ms 24004 KB Output is correct
63 Correct 231 ms 23972 KB Output is correct
64 Correct 269 ms 23928 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 59 ms 6152 KB Output is correct
67 Correct 234 ms 23924 KB Output is correct
68 Correct 238 ms 24040 KB Output is correct
69 Correct 240 ms 24044 KB Output is correct
70 Correct 1 ms 292 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 65 ms 6256 KB Output is correct
74 Correct 229 ms 24068 KB Output is correct
75 Correct 235 ms 24124 KB Output is correct
76 Correct 226 ms 23936 KB Output is correct
77 Correct 232 ms 23968 KB Output is correct
78 Correct 0 ms 288 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 6 ms 656 KB Output is correct
81 Correct 108 ms 10088 KB Output is correct
82 Correct 1 ms 300 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 58 ms 6252 KB Output is correct
86 Correct 96 ms 10092 KB Output is correct
87 Correct 97 ms 10100 KB Output is correct
88 Correct 99 ms 10088 KB Output is correct
89 Correct 93 ms 10104 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 5 ms 588 KB Output is correct
93 Correct 97 ms 10096 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 24 ms 2680 KB Output is correct
98 Correct 109 ms 10052 KB Output is correct
99 Correct 110 ms 10104 KB Output is correct
100 Correct 92 ms 10100 KB Output is correct
101 Correct 91 ms 10104 KB Output is correct
102 Correct 97 ms 10108 KB Output is correct
103 Correct 108 ms 10104 KB Output is correct
104 Correct 93 ms 10108 KB Output is correct
105 Correct 100 ms 10096 KB Output is correct