Submission #388499

# Submission time Handle Problem Language Result Execution time Memory
388499 2021-04-11T17:57:34 Z alexxela12345 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
294 ms 26120 KB
#include "supertrees.h"
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;

const int MAXN = 1e3;

int pa[MAXN];
int ra[MAXN];

void init() {
    iota(pa, pa + MAXN, 0);
    fill(ra, ra + MAXN, 0);
}

int get(int a) {
    if (a == pa[a])
        return a;
    return pa[a] = get(pa[a]);
}

void merge(int a, int b) {
    a = get(a);
    b = get(b);
    if (a == b)
        return;
    if (ra[b] > ra[a])
        swap(a, b);
    pa[b] = a;
    if (ra[a] == ra[b])
        ra[a]++;
}

vector<vector<int>> g;

vector<vector<int>> p2;

vector<int> comp;
vector<int> used;

int cur_time = 1;

void dfs(int v) {
    used[v] = cur_time;
    comp.push_back(v);
    for (int u : g[v]) {
        if (used[u] < cur_time) {
            dfs(u);
        }
    }
}

int construct(vector<vector<int>> p) {
    init();
    int n = p.size();
    used.resize(n);
    g.resize(n);
    p2.resize(n, vector<int> (n));
    vector<vector<int>> answer;
    for (int i = 0; i < n; i++) {
        vector<int> row;
        row.resize(n);
        answer.push_back(row);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 1) {
                if (get(i) != get(j)) {
                    merge(i, j);
                    comp.clear();
                    dfs(i);
                    cur_time++;
                    auto c1 = comp;
                    comp.clear();
                    dfs(j);
                    cur_time++;
                    auto c2 = comp;
                    for (int el1 : c1) {
                        for (int el2 : c2) {
                            p2[el2][el1] = 1;
                            p2[el1][el2] = 1;
                        }
                    }
                    g[i].push_back(j);
                    g[j].push_back(i);
                    answer[i][j] = 1;
                    answer[j][i] = 1;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        vector<int> arr = {i};
        bool bad = 0;
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 2) {
                if (get(i) == get(j)) {
                    bad = 1;
                    break;
                }
                arr.push_back(j);
            }
        }
        if (bad || arr.size() == 1)
            continue;
        if (arr.size() == 2) {
            return 0;
        }
        for (int &el : arr) {
            el = get(el);
        }
        sort(arr.begin(), arr.end());
        arr.resize(unique(arr.begin(), arr.end()) - arr.begin());
        vector<vector<int>> comps;
        for (auto el : arr) {
            comp.clear();
            dfs(el);
            cur_time++;
            comps.push_back(comp);
        }
        for (int i = 0; i < (int) comps.size(); i++) {
            for (int j = i + 1; j < (int) comps.size(); j++) {
                for (int el1 : comps[i]) {
                    for (int el2 : comps[j]) {
                        p2[el1][el2] = p2[el2][el1] = 2;
                    }
                }
            }
        }
        for (int i = 0; i < (int) arr.size(); i++) {
            int a = arr[i];
            int b = arr[(i + 1) % arr.size()];
            merge(a, b);
            answer[a][b] = answer[b][a] = 1;
        }
    }
    for (int i = 0; i < n; i++) {
        p2[i][i] = 1;
    }
    if (p != p2) {
        return 0;
    }
    build(answer);
    return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 13 ms 1356 KB Output is correct
7 Correct 263 ms 25972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 13 ms 1356 KB Output is correct
7 Correct 263 ms 25972 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 10 ms 1356 KB Output is correct
13 Correct 246 ms 25988 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 138 ms 16124 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 62 ms 6780 KB Output is correct
21 Correct 294 ms 26036 KB Output is correct
22 Correct 246 ms 25924 KB Output is correct
23 Correct 266 ms 26092 KB Output is correct
24 Correct 269 ms 25976 KB Output is correct
25 Correct 121 ms 16116 KB Output is correct
26 Correct 112 ms 16120 KB Output is correct
27 Correct 269 ms 26120 KB Output is correct
28 Correct 251 ms 25980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 10 ms 1356 KB Output is correct
9 Correct 245 ms 26016 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 10 ms 1280 KB Output is correct
13 Correct 271 ms 26056 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 5 ms 844 KB Output is correct
17 Correct 116 ms 16132 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 62 ms 6892 KB Output is correct
22 Correct 247 ms 25972 KB Output is correct
23 Correct 258 ms 25976 KB Output is correct
24 Correct 261 ms 25960 KB Output is correct
25 Correct 109 ms 16068 KB Output is correct
26 Correct 118 ms 16000 KB Output is correct
27 Correct 246 ms 25976 KB Output is correct
28 Correct 256 ms 25960 KB Output is correct
29 Correct 110 ms 15996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 62 ms 6724 KB Output is correct
5 Correct 252 ms 26052 KB Output is correct
6 Correct 252 ms 25940 KB Output is correct
7 Correct 266 ms 26084 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 61 ms 6732 KB Output is correct
10 Correct 247 ms 25976 KB Output is correct
11 Correct 244 ms 25924 KB Output is correct
12 Correct 259 ms 25920 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 62 ms 6680 KB Output is correct
17 Correct 248 ms 25984 KB Output is correct
18 Correct 244 ms 25948 KB Output is correct
19 Correct 251 ms 25924 KB Output is correct
20 Correct 250 ms 25972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 13 ms 1356 KB Output is correct
7 Correct 263 ms 25972 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 10 ms 1356 KB Output is correct
13 Correct 246 ms 25988 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 138 ms 16124 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 62 ms 6780 KB Output is correct
21 Correct 294 ms 26036 KB Output is correct
22 Correct 246 ms 25924 KB Output is correct
23 Correct 266 ms 26092 KB Output is correct
24 Correct 269 ms 25976 KB Output is correct
25 Correct 121 ms 16116 KB Output is correct
26 Correct 112 ms 16120 KB Output is correct
27 Correct 269 ms 26120 KB Output is correct
28 Correct 251 ms 25980 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 10 ms 1356 KB Output is correct
37 Correct 245 ms 26016 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 10 ms 1280 KB Output is correct
41 Correct 271 ms 26056 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 116 ms 16132 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 62 ms 6892 KB Output is correct
50 Correct 247 ms 25972 KB Output is correct
51 Correct 258 ms 25976 KB Output is correct
52 Correct 261 ms 25960 KB Output is correct
53 Correct 109 ms 16068 KB Output is correct
54 Correct 118 ms 16000 KB Output is correct
55 Correct 246 ms 25976 KB Output is correct
56 Correct 256 ms 25960 KB Output is correct
57 Correct 110 ms 15996 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 6 ms 972 KB Output is correct
61 Correct 133 ms 16100 KB Output is correct
62 Correct 0 ms 204 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 62 ms 6772 KB Output is correct
66 Correct 132 ms 16004 KB Output is correct
67 Correct 110 ms 16068 KB Output is correct
68 Correct 111 ms 16124 KB Output is correct
69 Correct 129 ms 15988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 13 ms 1356 KB Output is correct
7 Correct 263 ms 25972 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 10 ms 1356 KB Output is correct
13 Correct 246 ms 25988 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 6 ms 972 KB Output is correct
17 Correct 138 ms 16124 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 62 ms 6780 KB Output is correct
21 Correct 294 ms 26036 KB Output is correct
22 Correct 246 ms 25924 KB Output is correct
23 Correct 266 ms 26092 KB Output is correct
24 Correct 269 ms 25976 KB Output is correct
25 Correct 121 ms 16116 KB Output is correct
26 Correct 112 ms 16120 KB Output is correct
27 Correct 269 ms 26120 KB Output is correct
28 Correct 251 ms 25980 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 0 ms 204 KB Output is correct
31 Correct 0 ms 204 KB Output is correct
32 Correct 0 ms 204 KB Output is correct
33 Correct 0 ms 204 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 10 ms 1356 KB Output is correct
37 Correct 245 ms 26016 KB Output is correct
38 Correct 0 ms 204 KB Output is correct
39 Correct 0 ms 204 KB Output is correct
40 Correct 10 ms 1280 KB Output is correct
41 Correct 271 ms 26056 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 5 ms 844 KB Output is correct
45 Correct 116 ms 16132 KB Output is correct
46 Correct 0 ms 204 KB Output is correct
47 Correct 0 ms 204 KB Output is correct
48 Correct 0 ms 204 KB Output is correct
49 Correct 62 ms 6892 KB Output is correct
50 Correct 247 ms 25972 KB Output is correct
51 Correct 258 ms 25976 KB Output is correct
52 Correct 261 ms 25960 KB Output is correct
53 Correct 109 ms 16068 KB Output is correct
54 Correct 118 ms 16000 KB Output is correct
55 Correct 246 ms 25976 KB Output is correct
56 Correct 256 ms 25960 KB Output is correct
57 Correct 110 ms 15996 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 0 ms 204 KB Output is correct
61 Correct 62 ms 6724 KB Output is correct
62 Correct 252 ms 26052 KB Output is correct
63 Correct 252 ms 25940 KB Output is correct
64 Correct 266 ms 26084 KB Output is correct
65 Correct 0 ms 204 KB Output is correct
66 Correct 61 ms 6732 KB Output is correct
67 Correct 247 ms 25976 KB Output is correct
68 Correct 244 ms 25924 KB Output is correct
69 Correct 259 ms 25920 KB Output is correct
70 Correct 1 ms 204 KB Output is correct
71 Correct 0 ms 204 KB Output is correct
72 Correct 0 ms 204 KB Output is correct
73 Correct 62 ms 6680 KB Output is correct
74 Correct 248 ms 25984 KB Output is correct
75 Correct 244 ms 25948 KB Output is correct
76 Correct 251 ms 25924 KB Output is correct
77 Correct 250 ms 25972 KB Output is correct
78 Correct 0 ms 204 KB Output is correct
79 Correct 0 ms 204 KB Output is correct
80 Correct 6 ms 972 KB Output is correct
81 Correct 133 ms 16100 KB Output is correct
82 Correct 0 ms 204 KB Output is correct
83 Correct 0 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 62 ms 6772 KB Output is correct
86 Correct 132 ms 16004 KB Output is correct
87 Correct 110 ms 16068 KB Output is correct
88 Correct 111 ms 16124 KB Output is correct
89 Correct 129 ms 15988 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 0 ms 204 KB Output is correct
92 Correct 5 ms 976 KB Output is correct
93 Correct 125 ms 16068 KB Output is correct
94 Correct 0 ms 204 KB Output is correct
95 Correct 0 ms 208 KB Output is correct
96 Correct 0 ms 204 KB Output is correct
97 Correct 28 ms 4220 KB Output is correct
98 Correct 111 ms 15988 KB Output is correct
99 Correct 115 ms 16120 KB Output is correct
100 Correct 111 ms 16124 KB Output is correct
101 Correct 110 ms 15992 KB Output is correct
102 Correct 113 ms 15988 KB Output is correct
103 Correct 112 ms 16120 KB Output is correct
104 Correct 132 ms 16120 KB Output is correct
105 Correct 120 ms 16000 KB Output is correct