Submission #367010

# Submission time Handle Problem Language Result Execution time Memory
367010 2021-02-16T03:15:39 Z PurpleCrayon Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
259 ms 23788 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

#define ar array
#define sz(v) int(v.size())

int n;

struct DSU {
    vector<int> p, sz;
    void init(int n){ sz.assign(n, 1); p.resize(n); iota(p.begin(), p.end(), 0); }
    int find_set(int v){ return v==p[v]?v:p[v]=find_set(p[v]); }
    bool union_sets(int a, int b){
        if ((a=find_set(a))==(b=find_set(b))) return 0;
        if (sz[a] < sz[b]) swap(a, b);
        p[b]=a, sz[a]+=sz[b], sz[b]=0;
        return 1;
    }
} d;

int construct(vector<vector<int>> g) {
	n = sz(g);
    vector<vector<int>> ans(n, vector<int>(n));


    bool done=0, bad=0;

    [&](){ //handle case with a 3
        if (done || bad) return;

        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (g[i][j] == 3) {
            bad=1; return;
        }
    }();

    
    [&](){ //only 1's (tree)
        if (done || bad) return;

        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j] != 1) return;
        for (int i = 1; i < n; i++) ans[i][0] = ans[0][i] = 1;

        build(ans), done=1;
    }();

    [&](){ //0, 1's (just deal with separate comps)
        if (done || bad) return;

        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j] != 0 && g[i][j] != 1) return;

        d.init(n);
        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j])
            if (d.union_sets(i, j)) ans[i][j] = ans[j][i] = 1;

        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++)
            if ((d.find_set(i) == d.find_set(j))^bool(g[i][j])) return;

        build(ans), done=1;
    }();

    [&](){ //0, 2's (just deal with separate comps, put everything into cycles instead of trees)
        if (done || bad) return;

        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j] != 0 && g[i][j] != 2) return;

        d.init(n);
        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j])
            d.union_sets(i, j);

        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++)
            if ((d.find_set(i) == d.find_set(j))^bool(g[i][j])) return;

        vector<vector<int>> comp(n);
        for (int i = 0; i < n; i++) comp[d.find_set(i)].push_back(i);

        for (int i = 0; i < n; i++) if (sz(comp[i]) > 1){
            if (sz(comp[i]) == 2) return;
            for (int j = 0; j < sz(comp[i]); j++){
                int nxt=(j+1)%sz(comp[i]);
                ans[comp[i][j]][comp[i][nxt]] = ans[comp[i][nxt]][comp[i][j]] = 1;
            }
        }

        build(ans), done=1;
    }();

    [&](){ //full sol
        if (done || bad) return;

        d.init(n);
        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++) if (g[i][j])
            d.union_sets(i, j);

        for (int i = 0; i < n; i++) for (int j = i+1; j < n; j++)
            if ((d.find_set(i) == d.find_set(j))^bool(g[i][j])) return;

        vector<vector<int>> comp(n);
        for (int i = 0; i < n; i++) comp[d.find_set(i)].push_back(i);

        d.init(n);
        auto solve_comp = [&](vector<int> v) -> bool { //they're all in a component
            for (int i : v) for (int j : v) if (i > j && g[i][j]){
                if (g[i][j] == 1){
                    if (d.union_sets(i, j)) ans[i][j] = ans[j][i] = 1;
                } else if (g[i][j] == 2) {
                } else assert(false);
            }
            for (int i : v) for (int j : v) if (i > j && g[i][j] == 2 && d.find_set(i) == d.find_set(j))
                return 0;

            set<int> s;
            for (int i : v) s.insert(d.find_set(i));

            if (sz(s) == 1) return 1;
            else if (sz(s) == 2) return 0;

            vector<int> root(s.begin(), s.end());

            for (int i = 0; i < sz(root); i++){
                int nxt=(i+1)%(sz(root));
                ans[root[i]][root[nxt]] = ans[root[nxt]][root[i]] = 1;
            }
            return 1;
        };

        for (int i = 0; i < n; i++) if (sz(comp[i]) > 1)
            if (!solve_comp(comp[i])) return;

        build(ans), done=1;
    }();

	return !bad && done;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 237 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 237 ms 22124 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 250 ms 22260 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 114 ms 12140 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 63 ms 5868 KB Output is correct
21 Correct 251 ms 22252 KB Output is correct
22 Correct 257 ms 22124 KB Output is correct
23 Correct 252 ms 22240 KB Output is correct
24 Correct 252 ms 22124 KB Output is correct
25 Correct 107 ms 12140 KB Output is correct
26 Correct 103 ms 12140 KB Output is correct
27 Correct 257 ms 22124 KB Output is correct
28 Correct 249 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 11 ms 1260 KB Output is correct
9 Correct 249 ms 22112 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 0 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 243 ms 22252 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 114 ms 12268 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 63 ms 5996 KB Output is correct
22 Correct 254 ms 22124 KB Output is correct
23 Correct 251 ms 22124 KB Output is correct
24 Correct 256 ms 22124 KB Output is correct
25 Correct 108 ms 12268 KB Output is correct
26 Correct 110 ms 12140 KB Output is correct
27 Correct 253 ms 22124 KB Output is correct
28 Correct 254 ms 22112 KB Output is correct
29 Correct 107 ms 12268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 63 ms 5868 KB Output is correct
5 Correct 249 ms 22252 KB Output is correct
6 Correct 249 ms 22124 KB Output is correct
7 Correct 253 ms 22252 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 67 ms 5868 KB Output is correct
10 Correct 252 ms 22124 KB Output is correct
11 Correct 250 ms 22124 KB Output is correct
12 Correct 252 ms 22124 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 65 ms 6380 KB Output is correct
17 Correct 252 ms 23660 KB Output is correct
18 Correct 259 ms 23788 KB Output is correct
19 Correct 252 ms 23660 KB Output is correct
20 Correct 252 ms 23788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 237 ms 22124 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 250 ms 22260 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 114 ms 12140 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 63 ms 5868 KB Output is correct
21 Correct 251 ms 22252 KB Output is correct
22 Correct 257 ms 22124 KB Output is correct
23 Correct 252 ms 22240 KB Output is correct
24 Correct 252 ms 22124 KB Output is correct
25 Correct 107 ms 12140 KB Output is correct
26 Correct 103 ms 12140 KB Output is correct
27 Correct 257 ms 22124 KB Output is correct
28 Correct 249 ms 22124 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 0 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1260 KB Output is correct
37 Correct 249 ms 22112 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 0 ms 364 KB Output is correct
40 Correct 10 ms 1260 KB Output is correct
41 Correct 243 ms 22252 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 114 ms 12268 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 0 ms 364 KB Output is correct
49 Correct 63 ms 5996 KB Output is correct
50 Correct 254 ms 22124 KB Output is correct
51 Correct 251 ms 22124 KB Output is correct
52 Correct 256 ms 22124 KB Output is correct
53 Correct 108 ms 12268 KB Output is correct
54 Correct 110 ms 12140 KB Output is correct
55 Correct 253 ms 22124 KB Output is correct
56 Correct 254 ms 22112 KB Output is correct
57 Correct 107 ms 12268 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 5 ms 748 KB Output is correct
61 Correct 106 ms 12140 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 66 ms 5996 KB Output is correct
66 Correct 116 ms 13676 KB Output is correct
67 Correct 104 ms 13676 KB Output is correct
68 Correct 103 ms 13676 KB Output is correct
69 Correct 108 ms 13804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 237 ms 22124 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 250 ms 22260 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 114 ms 12140 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 63 ms 5868 KB Output is correct
21 Correct 251 ms 22252 KB Output is correct
22 Correct 257 ms 22124 KB Output is correct
23 Correct 252 ms 22240 KB Output is correct
24 Correct 252 ms 22124 KB Output is correct
25 Correct 107 ms 12140 KB Output is correct
26 Correct 103 ms 12140 KB Output is correct
27 Correct 257 ms 22124 KB Output is correct
28 Correct 249 ms 22124 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 0 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1260 KB Output is correct
37 Correct 249 ms 22112 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 0 ms 364 KB Output is correct
40 Correct 10 ms 1260 KB Output is correct
41 Correct 243 ms 22252 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 114 ms 12268 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 0 ms 364 KB Output is correct
49 Correct 63 ms 5996 KB Output is correct
50 Correct 254 ms 22124 KB Output is correct
51 Correct 251 ms 22124 KB Output is correct
52 Correct 256 ms 22124 KB Output is correct
53 Correct 108 ms 12268 KB Output is correct
54 Correct 110 ms 12140 KB Output is correct
55 Correct 253 ms 22124 KB Output is correct
56 Correct 254 ms 22112 KB Output is correct
57 Correct 107 ms 12268 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 63 ms 5868 KB Output is correct
62 Correct 249 ms 22252 KB Output is correct
63 Correct 249 ms 22124 KB Output is correct
64 Correct 253 ms 22252 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 67 ms 5868 KB Output is correct
67 Correct 252 ms 22124 KB Output is correct
68 Correct 250 ms 22124 KB Output is correct
69 Correct 252 ms 22124 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 65 ms 6380 KB Output is correct
74 Correct 252 ms 23660 KB Output is correct
75 Correct 259 ms 23788 KB Output is correct
76 Correct 252 ms 23660 KB Output is correct
77 Correct 252 ms 23788 KB Output is correct
78 Correct 0 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 5 ms 748 KB Output is correct
81 Correct 106 ms 12140 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 66 ms 5996 KB Output is correct
86 Correct 116 ms 13676 KB Output is correct
87 Correct 104 ms 13676 KB Output is correct
88 Correct 103 ms 13676 KB Output is correct
89 Correct 108 ms 13804 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 492 KB Output is correct
92 Correct 5 ms 876 KB Output is correct
93 Correct 102 ms 13776 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 26 ms 3820 KB Output is correct
98 Correct 102 ms 13676 KB Output is correct
99 Correct 104 ms 13804 KB Output is correct
100 Correct 102 ms 13676 KB Output is correct
101 Correct 103 ms 13676 KB Output is correct
102 Correct 94 ms 13676 KB Output is correct
103 Correct 96 ms 13804 KB Output is correct
104 Correct 95 ms 13676 KB Output is correct
105 Correct 105 ms 13804 KB Output is correct