Submission #1071505

# Submission time Handle Problem Language Result Execution time Memory
1071505 2024-08-23T07:55:38 Z Ignut Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
146 ms 22356 KB
// Ignut

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int P = 5;
const int MOD = 1e9 + 9;

int add(int a, int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}

int mult(int a, int b) {
    return 1ll * a * b % MOD;
}

// ===================================================================== //

void build(vector<vector<int>> b);

struct DSU {
    vector<int> p;
    vector<vector<int>> comp;
    void Init(int n) {
        p.clear(), comp.clear();
        for (int i = 0; i < n; i ++) p.push_back(i), comp.push_back({i});
    }
    int Get(int v) {
        return p[v] == v ? v : p[v] = Get(p[v]);
    }
    void Unite(int u, int v) {
        u = Get(u), v = Get(v);
        if (u == v) return;
        if (comp[u].size() > comp[v].size()) swap(u, v);
        p[u] = v;
        for (int val : comp[u]) comp[v].push_back(val);
    }
};

int construct(vector<vector<int>> p) {
    int n = p.size();
    vector<vector<int>> b(n);
    for (int i = 0; i < n; i ++) b[i].assign(n, 0);
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++) {
            if (p[i][j] == 3)
                return 0;
        }
    }

    // ================= chains =========================== //

    bool have1[n] = {};
    int h[n];
    for (int i = 0; i < n; i ++) {
        int hsh = 0;
        int ppow = 1;
        for (int j = 0; j < n; j ++) {
            ppow = mult(ppow, P);
            hsh = add(hsh, mult(ppow, p[i][j]));
            have1[i] |= p[i][j] == 1;
        }
        h[i] = hsh;
    }
    DSU chains; chains.Init(n);
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++) {
            if (h[i] == h[j] && have1[i])
                chains.Unite(i, j);
        }
    }
    map<int, int> chain;
    int nextChain = 1;
    for (int i = 0; i < n; i ++) {
        if (chains.Get(i) != i) continue;
        if (chains.comp[i].size() == 1) continue;
        for (int v : chains.comp[i]) chain[v] = nextChain;
        nextChain ++;
        for (int j = 0; j + 1 < chains.comp[i].size(); j ++) {
            int u = chains.comp[i][j];
            int v = chains.comp[i][j + 1];
            b[u][v] = b[v][u] = true;
        }
    }

    // ================== cycles ========================== //

    DSU dsu; dsu.Init(n);
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++) {
            if (p[i][j] > 0) {
                dsu.Unite(i, j);
            }
        }
    }
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < n; j ++) {
            bool f1 = (p[i][j] > 0);
            bool f2 = (dsu.Get(i) == dsu.Get(j));
            if (f1 != f2)
                return 0;
        }
    }

    for (int i = 0; i < n; i ++) {
        if (dsu.Get(i) != i) continue;
        // if (dsu.comp[i].size() == 1) continue;
        // if (dsu.comp[i].size() <= 2)
        //     return 0;
        // // cerr << dsu.comp[i].size() << '\n';
        // for (int j = 0; j < dsu.comp[i].size(); j ++) {
        //     int u = dsu.comp[i][j], v = dsu.comp[i][(j + 1) % dsu.comp[i].size()];
        //     b[u][v] = b[v][u] = 1;
        // }
        map<int, int> usedChain;
        vector<int> comp;
        for (int v : dsu.comp[i]) {
            if (!chain.count(v)) {
                comp.push_back(v);
                continue;
            }
            int ch = chain[v];
            if (usedChain.count(ch)) {
                continue;
            }
            usedChain[ch] ++;
            comp.push_back(v);
        }

        if (comp.size() == 1)
            continue;
        if (comp.size() == 2)
            return 0;
        
        for (int j = 0; j < comp.size(); j ++) {
            int u = comp[j];
            int v = comp[(j + 1) % comp.size()];
            b[u][v] = b[v][u] = true;
        }
    }

    build(b);
    return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:81:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |         for (int j = 0; j + 1 < chains.comp[i].size(); j ++) {
      |                         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:137:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  137 |         for (int j = 0; j < comp.size(); j ++) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1300 KB Output is correct
7 Correct 139 ms 22352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1300 KB Output is correct
7 Correct 139 ms 22352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1116 KB Output is correct
13 Correct 125 ms 22100 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 71 ms 12372 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 34 ms 5780 KB Output is correct
21 Correct 129 ms 22352 KB Output is correct
22 Correct 133 ms 22100 KB Output is correct
23 Correct 137 ms 22112 KB Output is correct
24 Correct 122 ms 22100 KB Output is correct
25 Correct 57 ms 12368 KB Output is correct
26 Correct 55 ms 12368 KB Output is correct
27 Correct 139 ms 22280 KB Output is correct
28 Correct 122 ms 22096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 5 ms 1112 KB Output is correct
9 Correct 129 ms 22292 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1116 KB Output is correct
13 Correct 134 ms 22112 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 956 KB Output is correct
17 Correct 75 ms 12372 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 32 ms 5776 KB Output is correct
22 Correct 129 ms 22356 KB Output is correct
23 Correct 122 ms 22296 KB Output is correct
24 Correct 131 ms 22100 KB Output is correct
25 Correct 57 ms 12372 KB Output is correct
26 Correct 56 ms 12308 KB Output is correct
27 Correct 138 ms 22100 KB Output is correct
28 Correct 131 ms 22100 KB Output is correct
29 Correct 58 ms 12372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 32 ms 5768 KB Output is correct
5 Correct 123 ms 22252 KB Output is correct
6 Correct 124 ms 22104 KB Output is correct
7 Correct 146 ms 22276 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 31 ms 5724 KB Output is correct
10 Correct 124 ms 22132 KB Output is correct
11 Correct 131 ms 22100 KB Output is correct
12 Correct 133 ms 22292 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 31 ms 5884 KB Output is correct
17 Correct 127 ms 22096 KB Output is correct
18 Correct 141 ms 22292 KB Output is correct
19 Correct 133 ms 22296 KB Output is correct
20 Correct 132 ms 22100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1300 KB Output is correct
7 Correct 139 ms 22352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1116 KB Output is correct
13 Correct 125 ms 22100 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 71 ms 12372 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 34 ms 5780 KB Output is correct
21 Correct 129 ms 22352 KB Output is correct
22 Correct 133 ms 22100 KB Output is correct
23 Correct 137 ms 22112 KB Output is correct
24 Correct 122 ms 22100 KB Output is correct
25 Correct 57 ms 12368 KB Output is correct
26 Correct 55 ms 12368 KB Output is correct
27 Correct 139 ms 22280 KB Output is correct
28 Correct 122 ms 22096 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 5 ms 1112 KB Output is correct
37 Correct 129 ms 22292 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 6 ms 1116 KB Output is correct
41 Correct 134 ms 22112 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 956 KB Output is correct
45 Correct 75 ms 12372 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 32 ms 5776 KB Output is correct
50 Correct 129 ms 22356 KB Output is correct
51 Correct 122 ms 22296 KB Output is correct
52 Correct 131 ms 22100 KB Output is correct
53 Correct 57 ms 12372 KB Output is correct
54 Correct 56 ms 12308 KB Output is correct
55 Correct 138 ms 22100 KB Output is correct
56 Correct 131 ms 22100 KB Output is correct
57 Correct 58 ms 12372 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 74 ms 12372 KB Output is correct
62 Correct 0 ms 344 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 30 ms 5776 KB Output is correct
66 Correct 64 ms 12308 KB Output is correct
67 Correct 62 ms 12308 KB Output is correct
68 Correct 55 ms 12324 KB Output is correct
69 Correct 58 ms 12372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 6 ms 1300 KB Output is correct
7 Correct 139 ms 22352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 6 ms 1116 KB Output is correct
13 Correct 125 ms 22100 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 71 ms 12372 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 34 ms 5780 KB Output is correct
21 Correct 129 ms 22352 KB Output is correct
22 Correct 133 ms 22100 KB Output is correct
23 Correct 137 ms 22112 KB Output is correct
24 Correct 122 ms 22100 KB Output is correct
25 Correct 57 ms 12368 KB Output is correct
26 Correct 55 ms 12368 KB Output is correct
27 Correct 139 ms 22280 KB Output is correct
28 Correct 122 ms 22096 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 5 ms 1112 KB Output is correct
37 Correct 129 ms 22292 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 6 ms 1116 KB Output is correct
41 Correct 134 ms 22112 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 956 KB Output is correct
45 Correct 75 ms 12372 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 32 ms 5776 KB Output is correct
50 Correct 129 ms 22356 KB Output is correct
51 Correct 122 ms 22296 KB Output is correct
52 Correct 131 ms 22100 KB Output is correct
53 Correct 57 ms 12372 KB Output is correct
54 Correct 56 ms 12308 KB Output is correct
55 Correct 138 ms 22100 KB Output is correct
56 Correct 131 ms 22100 KB Output is correct
57 Correct 58 ms 12372 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 32 ms 5768 KB Output is correct
62 Correct 123 ms 22252 KB Output is correct
63 Correct 124 ms 22104 KB Output is correct
64 Correct 146 ms 22276 KB Output is correct
65 Correct 1 ms 344 KB Output is correct
66 Correct 31 ms 5724 KB Output is correct
67 Correct 124 ms 22132 KB Output is correct
68 Correct 131 ms 22100 KB Output is correct
69 Correct 133 ms 22292 KB Output is correct
70 Correct 0 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 31 ms 5884 KB Output is correct
74 Correct 127 ms 22096 KB Output is correct
75 Correct 141 ms 22292 KB Output is correct
76 Correct 133 ms 22296 KB Output is correct
77 Correct 132 ms 22100 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 74 ms 12372 KB Output is correct
82 Correct 0 ms 344 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 30 ms 5776 KB Output is correct
86 Correct 64 ms 12308 KB Output is correct
87 Correct 62 ms 12308 KB Output is correct
88 Correct 55 ms 12324 KB Output is correct
89 Correct 58 ms 12372 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 3 ms 800 KB Output is correct
93 Correct 53 ms 12060 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 1 ms 348 KB Output is correct
96 Correct 1 ms 348 KB Output is correct
97 Correct 14 ms 3676 KB Output is correct
98 Correct 54 ms 14160 KB Output is correct
99 Correct 50 ms 14164 KB Output is correct
100 Correct 66 ms 14160 KB Output is correct
101 Correct 54 ms 14072 KB Output is correct
102 Correct 51 ms 14164 KB Output is correct
103 Correct 55 ms 14160 KB Output is correct
104 Correct 51 ms 14196 KB Output is correct
105 Correct 61 ms 14164 KB Output is correct