Submission #400105

# Submission time Handle Problem Language Result Execution time Memory
400105 2021-05-07T10:58:21 Z luisgalan Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
369 ms 24188 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << (x) << endl

const int MAX_N = 1e3 + 3;

int find(int x, vector<int> &ufds) {
    return ufds[x] = (ufds[x] == x ? x : find(ufds[x], ufds));
}
void connect(int a, int b, vector<int> &ufds) {
    if (rand() % 2) swap(a, b);
    ufds[find(a, ufds)] = find(b, ufds);
}

int construct(std::vector<std::vector<int>> p) {
    int n = p.size();
    vector<vector<int>> result(n);
    fill(result.begin(), result.end(), vector<int>(n));
    if (n == 1) {
        build(result);
        return 1;
    }

    vector<int> trees(n);
    vector<int> components(n);
    for (int i = 0; i < n; i++) {
        trees[i] = i;
        components[i] = i;
    }

    bool possible = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (p[i][j] == 1) {
                connect(i, j, trees);
            } else if (find(i, trees) == find(j, trees)) {
                possible = false;
            }
            if (p[i][j] > 0) {
                connect(i, j, components);
            } else if (find(i, components) == find(j, components)) {
                possible = false;
            }
        }
    }

    // for (int i = 0; i < n; i++) {
    //     cerr << trees[i] << ", ";
    // }
    // cerr << endl;
    // for (int i = 0; i < n; i++) {
    //     cerr << components[i] << ", ";
    // }
    // cerr << endl;

    vector<int> size(n);
    for (int i = 0; i < n; i++) {
        size[find(i, components)]++;
    }

    for (int i = 0; i < n; i++) {
        int a = find(i, trees);
        if (i == a) continue;
        result[i][a] = 1;
        result[a][i] = 1;
    }

    vector<vector<int>> cycles(n);
    set<int> visited;
    for (int i = 0; i < n; i++) {
        int t = find(i, trees);
        int c = find(i, components);
        if (!visited.count(t)) {
            cycles[c].push_back(t);
            visited.insert(t);
        }
    }

    for (int i = 0; i < n; i++) {
        vector<int> &cycle = cycles[i];
        if (cycle.size() <= 2) continue;
        for (int j = 0; j < cycle.size(); j++) {
            int a = cycle[j];
            int b = cycle[(j + 1) % cycle.size()];
            result[a][b] = 1;
            result[b][a] = 1;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (find(i, components) == find(j, components)) {
                if (find(i, trees) == find(j, trees)) {
                    possible = possible && p[i][j] == 1;
                } else {
                    possible = possible && p[i][j] == 2;
                    possible = possible && (size[find(i, components)] > 2);
                    // debug(i);
                    // debug(find(i, trees));
                    // debug(size[find(i, trees)]);
                }
            } else {
                possible = possible && p[i][j] == 0;
            }
        }
    }

    if (possible) {
        build(result);
    }
    return possible;
}

// it's easy to prove that there must be at most one cycle for st5 (and that any matrix with some p[i][j] = 2 must have a cycle)
// but we could have more than one tree
// we essentially have that some nodes are part of the cycle and then we have multiple trees rooted in one of the cycle nodes
// we could have nodes 1 2 3 and 4 where p[1][2] = 1, p[3][4] = 1 but p[2][3] = 2
// we have that p[i][j] = 2 iff the paths go through the cycle

// we essentially have a collection of trees
// if p[i][j] = 2 then node i and j must be on the same component but of different trees
// if p[i][j] = 1 then node i and j belong to the same tree
// it doesn't actually matter which node we root the tree in (ie which node we put on the cycle)
// we thus have the following algorithm:
// if p[i][j] = 1 connect them in the tree ufds
// if p[i][j] > 0 connect them in the component ufds
// for each node A in the same component connect find(A) to the cycle

// impossibility check:
// verify that all pairs (a, b) in the same tree have p[a][b] = 1
// verify that all pairs (a, b) in different trees have p[a][b] = 2

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:83:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for (int j = 0; j < cycle.size(); 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 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 16 ms 1208 KB Output is correct
7 Correct 366 ms 22580 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 16 ms 1208 KB Output is correct
7 Correct 366 ms 22580 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 296 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 281 ms 22596 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 10 ms 860 KB Output is correct
17 Correct 223 ms 14064 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 80 ms 6184 KB Output is correct
21 Correct 316 ms 23932 KB Output is correct
22 Correct 309 ms 24004 KB Output is correct
23 Correct 355 ms 24004 KB Output is correct
24 Correct 272 ms 24004 KB Output is correct
25 Correct 186 ms 14072 KB Output is correct
26 Correct 163 ms 14060 KB Output is correct
27 Correct 366 ms 24132 KB Output is correct
28 Correct 269 ms 24064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 296 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 12 ms 1228 KB Output is correct
9 Correct 266 ms 23996 KB Output is correct
10 Correct 1 ms 300 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 14 ms 1172 KB Output is correct
13 Correct 324 ms 23984 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 292 KB Output is correct
16 Correct 9 ms 860 KB Output is correct
17 Correct 183 ms 14072 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 296 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 75 ms 6212 KB Output is correct
22 Correct 303 ms 24048 KB Output is correct
23 Correct 297 ms 24188 KB Output is correct
24 Correct 322 ms 24004 KB Output is correct
25 Correct 132 ms 14148 KB Output is correct
26 Correct 161 ms 14076 KB Output is correct
27 Correct 300 ms 24060 KB Output is correct
28 Correct 324 ms 24004 KB Output is correct
29 Correct 137 ms 14276 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 78 ms 6020 KB Output is correct
5 Correct 321 ms 22656 KB Output is correct
6 Correct 298 ms 22596 KB Output is correct
7 Correct 369 ms 22632 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 77 ms 6000 KB Output is correct
10 Correct 301 ms 22536 KB Output is correct
11 Correct 294 ms 22676 KB Output is correct
12 Correct 322 ms 22640 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 75 ms 5964 KB Output is correct
17 Correct 301 ms 22624 KB Output is correct
18 Correct 313 ms 22532 KB Output is correct
19 Correct 315 ms 22612 KB Output is correct
20 Correct 280 ms 22580 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 16 ms 1208 KB Output is correct
7 Correct 366 ms 22580 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 296 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 281 ms 22596 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 10 ms 860 KB Output is correct
17 Correct 223 ms 14064 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 80 ms 6184 KB Output is correct
21 Correct 316 ms 23932 KB Output is correct
22 Correct 309 ms 24004 KB Output is correct
23 Correct 355 ms 24004 KB Output is correct
24 Correct 272 ms 24004 KB Output is correct
25 Correct 186 ms 14072 KB Output is correct
26 Correct 163 ms 14060 KB Output is correct
27 Correct 366 ms 24132 KB Output is correct
28 Correct 269 ms 24064 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 296 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 12 ms 1228 KB Output is correct
37 Correct 266 ms 23996 KB Output is correct
38 Correct 1 ms 300 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 14 ms 1172 KB Output is correct
41 Correct 324 ms 23984 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 292 KB Output is correct
44 Correct 9 ms 860 KB Output is correct
45 Correct 183 ms 14072 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 75 ms 6212 KB Output is correct
50 Correct 303 ms 24048 KB Output is correct
51 Correct 297 ms 24188 KB Output is correct
52 Correct 322 ms 24004 KB Output is correct
53 Correct 132 ms 14148 KB Output is correct
54 Correct 161 ms 14076 KB Output is correct
55 Correct 300 ms 24060 KB Output is correct
56 Correct 324 ms 24004 KB Output is correct
57 Correct 137 ms 14276 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 10 ms 844 KB Output is correct
61 Correct 223 ms 14064 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 80 ms 6244 KB Output is correct
66 Correct 162 ms 14176 KB Output is correct
67 Correct 172 ms 14092 KB Output is correct
68 Correct 173 ms 14088 KB Output is correct
69 Correct 137 ms 14192 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 16 ms 1208 KB Output is correct
7 Correct 366 ms 22580 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 296 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 12 ms 1228 KB Output is correct
13 Correct 281 ms 22596 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 10 ms 860 KB Output is correct
17 Correct 223 ms 14064 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 80 ms 6184 KB Output is correct
21 Correct 316 ms 23932 KB Output is correct
22 Correct 309 ms 24004 KB Output is correct
23 Correct 355 ms 24004 KB Output is correct
24 Correct 272 ms 24004 KB Output is correct
25 Correct 186 ms 14072 KB Output is correct
26 Correct 163 ms 14060 KB Output is correct
27 Correct 366 ms 24132 KB Output is correct
28 Correct 269 ms 24064 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 296 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 12 ms 1228 KB Output is correct
37 Correct 266 ms 23996 KB Output is correct
38 Correct 1 ms 300 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 14 ms 1172 KB Output is correct
41 Correct 324 ms 23984 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 292 KB Output is correct
44 Correct 9 ms 860 KB Output is correct
45 Correct 183 ms 14072 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 296 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 75 ms 6212 KB Output is correct
50 Correct 303 ms 24048 KB Output is correct
51 Correct 297 ms 24188 KB Output is correct
52 Correct 322 ms 24004 KB Output is correct
53 Correct 132 ms 14148 KB Output is correct
54 Correct 161 ms 14076 KB Output is correct
55 Correct 300 ms 24060 KB Output is correct
56 Correct 324 ms 24004 KB Output is correct
57 Correct 137 ms 14276 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 78 ms 6020 KB Output is correct
62 Correct 321 ms 22656 KB Output is correct
63 Correct 298 ms 22596 KB Output is correct
64 Correct 369 ms 22632 KB Output is correct
65 Correct 1 ms 208 KB Output is correct
66 Correct 77 ms 6000 KB Output is correct
67 Correct 301 ms 22536 KB Output is correct
68 Correct 294 ms 22676 KB Output is correct
69 Correct 322 ms 22640 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 75 ms 5964 KB Output is correct
74 Correct 301 ms 22624 KB Output is correct
75 Correct 313 ms 22532 KB Output is correct
76 Correct 315 ms 22612 KB Output is correct
77 Correct 280 ms 22580 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 10 ms 844 KB Output is correct
81 Correct 223 ms 14064 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 80 ms 6244 KB Output is correct
86 Correct 162 ms 14176 KB Output is correct
87 Correct 172 ms 14092 KB Output is correct
88 Correct 173 ms 14088 KB Output is correct
89 Correct 137 ms 14192 KB Output is correct
90 Correct 1 ms 204 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 10 ms 860 KB Output is correct
93 Correct 217 ms 14136 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 204 KB Output is correct
97 Correct 41 ms 3780 KB Output is correct
98 Correct 168 ms 14148 KB Output is correct
99 Correct 179 ms 14020 KB Output is correct
100 Correct 172 ms 14064 KB Output is correct
101 Correct 134 ms 14076 KB Output is correct
102 Correct 179 ms 14064 KB Output is correct
103 Correct 180 ms 14060 KB Output is correct
104 Correct 223 ms 14196 KB Output is correct
105 Correct 186 ms 14064 KB Output is correct