Submission #401061

# Submission time Handle Problem Language Result Execution time Memory
401061 2021-05-09T09:55:21 Z dxz05 Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
296 ms 24060 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1111;

int par[MAXN][4], sz[MAXN][4];

int find_set(int x, int flag){
    if (x == par[x][flag]) return x;
    return par[x][flag] = find_set(par[x][flag], flag);
}

bool union_sets(int x, int y, int flag){
    x = find_set(x, flag);
    y = find_set(y, flag);
    if (x == y) return false;
    if (sz[x][flag] > sz[y][flag]) swap(x, y);
    par[x][flag] = y;
    sz[y][flag] += sz[x][flag];
    return true;
}

bool same(int i, int j, int flag){
    return find_set(i, flag) == find_set(j, flag);
}

vector<int> children[MAXN];


bool check(vector<vector<int>> &answer, vector<vector<int>> p){
    int n = answer.size();
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 3) return false;
            if (answer[i][j] == 1) union_sets(i, j, 3);
        }
    }

    vector<int> cnt2(n);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 0){
                if (same(i, j, 3)) return false;
            }
            if (p[i][j] == 1){
                if (!same(i, j, 3)) return false;
                if (!same(i, j, 1)) return false;
            }
            if (p[i][j] == 2){
                if (!same(i, j, 3)) return false;
                if (same(i, j, 1)) return false;

                cnt2[find_set(i, 3)]++;

            }
        }
    }

    for (int i = 0; i < n; i++){
        if (cnt2[i] == 2) return false;
    }

    return true;
}

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

    for (int i = 0; i < n; i++){
        par[i][1] = par[i][2] = par[i][3] = i;
        sz[i][1] = sz[i][2] = sz[i][3] = 1;
    }
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 1) union_sets(i, j, 1);
        }
    }

    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            if (p[i][j] == 2){
                int x = find_set(i, 1), y = find_set(j, 1);
                union_sets(x, y, 2);
            }
        }
    }

    for (int i = 0; i < n; i++){
        children[find_set(i, 2)].push_back(i);
    }

    for (int i = 0; i < n; i++){
        if (children[i].size() < 2) continue;
        children[i].push_back(children[i].front());
        for (int j = 0; j < children[i].size() - 1; j++){
            int x = children[i][j], y = children[i][j + 1];
            answer[x][y] = answer[y][x] = 1;
        }
    }

    for (int i = 0; i < n; i++){
        int x = find_set(i, 1);
        if (x != i) answer[i][x] = answer[x][i] = 1;
    }

    if (!check(answer, p)) return 0;

    build(answer);
    return 1;
}

/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:98:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         for (int j = 0; j < children[i].size() - 1; j++){
      |                         ~~^~~~~~~~~~~~~~~~~~~~~~~~
# 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 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1232 KB Output is correct
7 Correct 266 ms 22060 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 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1232 KB Output is correct
7 Correct 266 ms 22060 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 254 ms 22020 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 141 ms 16172 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 68 ms 5760 KB Output is correct
21 Correct 265 ms 22104 KB Output is correct
22 Correct 263 ms 22060 KB Output is correct
23 Correct 294 ms 22072 KB Output is correct
24 Correct 278 ms 22052 KB Output is correct
25 Correct 107 ms 16160 KB Output is correct
26 Correct 129 ms 16156 KB Output is correct
27 Correct 278 ms 22096 KB Output is correct
28 Correct 256 ms 22100 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 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 11 ms 1216 KB Output is correct
9 Correct 259 ms 22052 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1184 KB Output is correct
13 Correct 269 ms 22064 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 976 KB Output is correct
17 Correct 120 ms 16036 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 65 ms 5724 KB Output is correct
22 Correct 268 ms 22056 KB Output is correct
23 Correct 289 ms 22220 KB Output is correct
24 Correct 286 ms 22028 KB Output is correct
25 Correct 109 ms 16152 KB Output is correct
26 Correct 110 ms 18072 KB Output is correct
27 Correct 296 ms 23976 KB Output is correct
28 Correct 285 ms 24060 KB Output is correct
29 Correct 109 ms 18092 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 68 ms 5756 KB Output is correct
5 Correct 261 ms 22096 KB Output is correct
6 Correct 266 ms 22060 KB Output is correct
7 Correct 279 ms 22100 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 67 ms 5740 KB Output is correct
10 Correct 263 ms 22064 KB Output is correct
11 Correct 262 ms 22056 KB Output is correct
12 Correct 287 ms 22320 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 66 ms 5820 KB Output is correct
17 Correct 263 ms 22176 KB Output is correct
18 Correct 278 ms 22096 KB Output is correct
19 Correct 260 ms 22096 KB Output is correct
20 Correct 255 ms 22068 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 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1232 KB Output is correct
7 Correct 266 ms 22060 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 254 ms 22020 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 141 ms 16172 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 68 ms 5760 KB Output is correct
21 Correct 265 ms 22104 KB Output is correct
22 Correct 263 ms 22060 KB Output is correct
23 Correct 294 ms 22072 KB Output is correct
24 Correct 278 ms 22052 KB Output is correct
25 Correct 107 ms 16160 KB Output is correct
26 Correct 129 ms 16156 KB Output is correct
27 Correct 278 ms 22096 KB Output is correct
28 Correct 256 ms 22100 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 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 11 ms 1216 KB Output is correct
37 Correct 259 ms 22052 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 12 ms 1184 KB Output is correct
41 Correct 269 ms 22064 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 6 ms 976 KB Output is correct
45 Correct 120 ms 16036 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 65 ms 5724 KB Output is correct
50 Correct 268 ms 22056 KB Output is correct
51 Correct 289 ms 22220 KB Output is correct
52 Correct 286 ms 22028 KB Output is correct
53 Correct 109 ms 16152 KB Output is correct
54 Correct 110 ms 18072 KB Output is correct
55 Correct 296 ms 23976 KB Output is correct
56 Correct 285 ms 24060 KB Output is correct
57 Correct 109 ms 18092 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 320 KB Output is correct
60 Correct 6 ms 1080 KB Output is correct
61 Correct 127 ms 18116 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 1 ms 332 KB Output is correct
64 Correct 1 ms 324 KB Output is correct
65 Correct 82 ms 6188 KB Output is correct
66 Correct 111 ms 17988 KB Output is correct
67 Correct 107 ms 18116 KB Output is correct
68 Correct 124 ms 18056 KB Output is correct
69 Correct 109 ms 18116 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 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 11 ms 1232 KB Output is correct
7 Correct 266 ms 22060 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1228 KB Output is correct
13 Correct 254 ms 22020 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 141 ms 16172 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 68 ms 5760 KB Output is correct
21 Correct 265 ms 22104 KB Output is correct
22 Correct 263 ms 22060 KB Output is correct
23 Correct 294 ms 22072 KB Output is correct
24 Correct 278 ms 22052 KB Output is correct
25 Correct 107 ms 16160 KB Output is correct
26 Correct 129 ms 16156 KB Output is correct
27 Correct 278 ms 22096 KB Output is correct
28 Correct 256 ms 22100 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 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 11 ms 1216 KB Output is correct
37 Correct 259 ms 22052 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 12 ms 1184 KB Output is correct
41 Correct 269 ms 22064 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 6 ms 976 KB Output is correct
45 Correct 120 ms 16036 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 65 ms 5724 KB Output is correct
50 Correct 268 ms 22056 KB Output is correct
51 Correct 289 ms 22220 KB Output is correct
52 Correct 286 ms 22028 KB Output is correct
53 Correct 109 ms 16152 KB Output is correct
54 Correct 110 ms 18072 KB Output is correct
55 Correct 296 ms 23976 KB Output is correct
56 Correct 285 ms 24060 KB Output is correct
57 Correct 109 ms 18092 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 68 ms 5756 KB Output is correct
62 Correct 261 ms 22096 KB Output is correct
63 Correct 266 ms 22060 KB Output is correct
64 Correct 279 ms 22100 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 67 ms 5740 KB Output is correct
67 Correct 263 ms 22064 KB Output is correct
68 Correct 262 ms 22056 KB Output is correct
69 Correct 287 ms 22320 KB Output is correct
70 Correct 1 ms 332 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 208 KB Output is correct
73 Correct 66 ms 5820 KB Output is correct
74 Correct 263 ms 22176 KB Output is correct
75 Correct 278 ms 22096 KB Output is correct
76 Correct 260 ms 22096 KB Output is correct
77 Correct 255 ms 22068 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 320 KB Output is correct
80 Correct 6 ms 1080 KB Output is correct
81 Correct 127 ms 18116 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 324 KB Output is correct
85 Correct 82 ms 6188 KB Output is correct
86 Correct 111 ms 17988 KB Output is correct
87 Correct 107 ms 18116 KB Output is correct
88 Correct 124 ms 18056 KB Output is correct
89 Correct 109 ms 18116 KB Output is correct
90 Correct 1 ms 332 KB Output is correct
91 Correct 1 ms 332 KB Output is correct
92 Correct 5 ms 972 KB Output is correct
93 Correct 115 ms 18064 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 332 KB Output is correct
97 Correct 27 ms 4752 KB Output is correct
98 Correct 111 ms 17988 KB Output is correct
99 Correct 106 ms 18116 KB Output is correct
100 Correct 122 ms 18072 KB Output is correct
101 Correct 104 ms 18076 KB Output is correct
102 Correct 95 ms 18116 KB Output is correct
103 Correct 109 ms 18096 KB Output is correct
104 Correct 110 ms 18116 KB Output is correct
105 Correct 111 ms 18060 KB Output is correct