Submission #800119

# Submission time Handle Problem Language Result Execution time Memory
800119 2023-08-01T10:26:50 Z PixelCat Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
186 ms 22104 KB
#include "supertrees.h"

#ifdef NYAOWO
#include "grader.cpp"
#endif

#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
// #define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;

const int MAXN = 1000;
const int BASE = 7;
const int MOD = 998244353;

vector<vector<int>> answer;
inline void add_edge(int a, int b) {
    answer[a][b] = answer[b][a] = 1;
}

struct DSU {
    int p[MAXN + 10];
    void init() {
        memset(p, -1, sizeof(p));
    }
    int find(int n) {
        if(p[n] < 0) return n;
        return p[n] = find(p[n]);
    }
    bool uni(int a, int b) {
        a = find(a); b = find(b);
        if(a == b) return false;
        p[b] = a;
        return true;
    }
    bool same(int a, int b) {
        return find(a) == find(b);
    }
    bool lead(int n) {
        return p[n] < 0;
    }
} dsu;

bool solve_sun(vector<vector<int>> &p, vector<int> &cc) {
    int n = sz(cc);

    // phase 1: collect groups of tantacles
    vector<pii> vh;  // {hash, index}
    For(i, 0, n - 1) {
        int h = 0;
        For(j, 0, n - 1) {
            h = (h * BASE + p[cc[i]][cc[j]]) % MOD;
        }
        vh.eb(h, cc[i]);
    }
    sort(all(vh));
    vector<vector<int>> g;
    g.eb();
    g.back().eb(vh[0].S);
    For(i, 1, n - 1) {
        if(vh[i].F != vh[i - 1].F) g.eb();
        g.back().eb(vh[i].S);
    }
    vector<int> gid(MAXN, -1);
    For(i, 0, sz(g) - 1) {
        for(auto &j:g[i]) gid[j] = i;
    }

    // check: is that really possible?
    if(sz(g) == 2) return 0;
    for(auto &v1:cc) for(auto &v2:cc) if(v1 != v2) {
        if(gid[v1] == gid[v2] && p[v1][v2] != 1) return false;
        if(gid[v1] != gid[v2] && p[v1][v2] != 2) return false;
    }

    // phase 2-1: link cycle
    For(i, 1, sz(g) - 1) {
        add_edge(g[i].back(), g[i - 1].back());
    }
    add_edge(g.front().back(), g.back().back());

    // phase 2-2: link tantacles
    for(auto &i:g) {
        int rt = i.back();
        i.pop_back();
        for(auto &j:i) {
            add_edge(j, rt);
        }
    }

    return true;
}

int construct(vector<vector<int>> p) {
    int n = sz(p);
    For(i, 0, n - 1) For(j, 0, n - 1) if(p[i][j] == 3) return 0;
    answer = vector<vector<int>>(n, vector<int>(n, 0));

    // phase 1: find all CC
    dsu.init();
    For(i, 0, n - 1) For(j, 0, n - 1) {
        if(p[i][j]) dsu.uni(i, j);
    }
    vector<vector<int>> vcc;
    For(i, 0, n - 1) if(dsu.lead(i)) {
        vcc.eb();
        For(j, 0, n - 1) if(dsu.same(i, j)) {
            vcc.back().eb(j);
        }
    }

    // phase 2: solve for each CC
    for(vector<int> &cc:vcc) if(sz(cc) > 1) {
        int mask = 0;
        for(auto &v1:cc) for(auto &v2:cc) if(v1 != v2) {
            mask |= (1 << p[v1][v2]);
        }
        if(mask & 1) return 0;
        if(mask == 2) {
            int rt = cc.back();
            cc.pop_back();
            for(auto &i:cc) {
                add_edge(i, rt);
            }
        } else {
            if(!solve_sun(p, cc)) return 0;
        }
    }

    build(answer);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1116 KB Output is correct
7 Correct 161 ms 22060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1116 KB Output is correct
7 Correct 161 ms 22060 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 157 ms 22088 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 76 ms 12052 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 39 ms 5680 KB Output is correct
21 Correct 171 ms 22024 KB Output is correct
22 Correct 160 ms 22024 KB Output is correct
23 Correct 163 ms 22024 KB Output is correct
24 Correct 155 ms 22032 KB Output is correct
25 Correct 67 ms 12036 KB Output is correct
26 Correct 66 ms 12108 KB Output is correct
27 Correct 160 ms 22040 KB Output is correct
28 Correct 158 ms 22028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 7 ms 1108 KB Output is correct
9 Correct 158 ms 22092 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1104 KB Output is correct
13 Correct 163 ms 22028 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 77 ms 12052 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 39 ms 5764 KB Output is correct
22 Correct 170 ms 22036 KB Output is correct
23 Correct 154 ms 21948 KB Output is correct
24 Correct 186 ms 22024 KB Output is correct
25 Correct 63 ms 12164 KB Output is correct
26 Correct 65 ms 12040 KB Output is correct
27 Correct 153 ms 22032 KB Output is correct
28 Correct 166 ms 22036 KB Output is correct
29 Correct 65 ms 12168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 38 ms 5664 KB Output is correct
5 Correct 163 ms 21964 KB Output is correct
6 Correct 155 ms 22024 KB Output is correct
7 Correct 162 ms 21948 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 47 ms 5660 KB Output is correct
10 Correct 169 ms 22028 KB Output is correct
11 Correct 156 ms 22028 KB Output is correct
12 Correct 165 ms 22036 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 40 ms 5680 KB Output is correct
17 Correct 164 ms 22036 KB Output is correct
18 Correct 157 ms 22024 KB Output is correct
19 Correct 154 ms 22036 KB Output is correct
20 Correct 156 ms 22104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1116 KB Output is correct
7 Correct 161 ms 22060 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 157 ms 22088 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 76 ms 12052 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 39 ms 5680 KB Output is correct
21 Correct 171 ms 22024 KB Output is correct
22 Correct 160 ms 22024 KB Output is correct
23 Correct 163 ms 22024 KB Output is correct
24 Correct 155 ms 22032 KB Output is correct
25 Correct 67 ms 12036 KB Output is correct
26 Correct 66 ms 12108 KB Output is correct
27 Correct 160 ms 22040 KB Output is correct
28 Correct 158 ms 22028 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1108 KB Output is correct
37 Correct 158 ms 22092 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1104 KB Output is correct
41 Correct 163 ms 22028 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 77 ms 12052 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 39 ms 5764 KB Output is correct
50 Correct 170 ms 22036 KB Output is correct
51 Correct 154 ms 21948 KB Output is correct
52 Correct 186 ms 22024 KB Output is correct
53 Correct 63 ms 12164 KB Output is correct
54 Correct 65 ms 12040 KB Output is correct
55 Correct 153 ms 22032 KB Output is correct
56 Correct 166 ms 22036 KB Output is correct
57 Correct 65 ms 12168 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 3 ms 724 KB Output is correct
61 Correct 89 ms 12048 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 38 ms 5708 KB Output is correct
66 Correct 64 ms 12076 KB Output is correct
67 Correct 72 ms 12040 KB Output is correct
68 Correct 64 ms 12056 KB Output is correct
69 Correct 64 ms 12164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 7 ms 1116 KB Output is correct
7 Correct 161 ms 22060 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 157 ms 22088 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 76 ms 12052 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 39 ms 5680 KB Output is correct
21 Correct 171 ms 22024 KB Output is correct
22 Correct 160 ms 22024 KB Output is correct
23 Correct 163 ms 22024 KB Output is correct
24 Correct 155 ms 22032 KB Output is correct
25 Correct 67 ms 12036 KB Output is correct
26 Correct 66 ms 12108 KB Output is correct
27 Correct 160 ms 22040 KB Output is correct
28 Correct 158 ms 22028 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 7 ms 1108 KB Output is correct
37 Correct 158 ms 22092 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 7 ms 1104 KB Output is correct
41 Correct 163 ms 22028 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 3 ms 724 KB Output is correct
45 Correct 77 ms 12052 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 39 ms 5764 KB Output is correct
50 Correct 170 ms 22036 KB Output is correct
51 Correct 154 ms 21948 KB Output is correct
52 Correct 186 ms 22024 KB Output is correct
53 Correct 63 ms 12164 KB Output is correct
54 Correct 65 ms 12040 KB Output is correct
55 Correct 153 ms 22032 KB Output is correct
56 Correct 166 ms 22036 KB Output is correct
57 Correct 65 ms 12168 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 38 ms 5664 KB Output is correct
62 Correct 163 ms 21964 KB Output is correct
63 Correct 155 ms 22024 KB Output is correct
64 Correct 162 ms 21948 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 47 ms 5660 KB Output is correct
67 Correct 169 ms 22028 KB Output is correct
68 Correct 156 ms 22028 KB Output is correct
69 Correct 165 ms 22036 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 40 ms 5680 KB Output is correct
74 Correct 164 ms 22036 KB Output is correct
75 Correct 157 ms 22024 KB Output is correct
76 Correct 154 ms 22036 KB Output is correct
77 Correct 156 ms 22104 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 3 ms 724 KB Output is correct
81 Correct 89 ms 12048 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 38 ms 5708 KB Output is correct
86 Correct 64 ms 12076 KB Output is correct
87 Correct 72 ms 12040 KB Output is correct
88 Correct 64 ms 12056 KB Output is correct
89 Correct 64 ms 12164 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 212 KB Output is correct
92 Correct 3 ms 596 KB Output is correct
93 Correct 65 ms 8084 KB Output is correct
94 Correct 0 ms 212 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 0 ms 212 KB Output is correct
97 Correct 16 ms 2188 KB Output is correct
98 Correct 61 ms 8140 KB Output is correct
99 Correct 61 ms 8068 KB Output is correct
100 Correct 61 ms 8076 KB Output is correct
101 Correct 61 ms 8140 KB Output is correct
102 Correct 60 ms 8088 KB Output is correct
103 Correct 60 ms 8068 KB Output is correct
104 Correct 61 ms 8084 KB Output is correct
105 Correct 65 ms 8068 KB Output is correct