Submission #683767

# Submission time Handle Problem Language Result Execution time Memory
683767 2023-01-19T10:18:26 Z sharaelong Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
212 ms 24124 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

struct DisjointSet {
    int n;
    vector<int> parent, rank;
    DisjointSet(int _n) : n(_n) {
        parent.resize(n);
        iota(parent.begin(), parent.end(), 0);
        rank.resize(n, 0);
    }

    int find(int u) {
        return parent[u] = (u == parent[u] ? u : find(parent[u]));
    }

    void merge(int u, int v) {
        u = find(u); v = find(v);
        if (u == v) return;
        if (rank[u] > rank[v]) swap(u, v);
        parent[u] = v;
        if (rank[u] == rank[v]) ++rank[v];
    }
};

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

    DisjointSet dsu(n);
    DisjointSet dsu2(n);
    for (int i=0; i<n; ++i) {
        for (int j=i+1; j<n; ++j) {
            if (p[i][j] >= 1) {
                dsu.merge(i, j);
            }
            if (p[i][j] == 1) {
                dsu2.merge(i, j);
            }
        }
    }
    for (int i=0; i<n; ++i) {
        for (int j=i+1; j<n; ++j) {
            if (dsu.find(i) == dsu.find(j) && p[i][j] == 0) return 0;
            if (dsu2.find(i) == dsu2.find(j) && p[i][j] != 1) return 0;
            if (dsu.find(i) == dsu.find(j) && dsu2.find(i) != dsu2.find(j) && p[i][j] != 2) return 0;
        }
    }
    
    vector<vector<int>> ans(n, vector<int>(n, 0));
    vector<vector<int>> trees(n);
    for (int i=0; i<n; ++i) trees[dsu2.find(i)].push_back(i);
    for (int i=0; i<n; ++i) {
        // for (int x: trees[i]) cout << x << ' ';
        // cout << endl;
        for (int j=0; j+1<trees[i].size(); ++j) {
            int x = trees[i][j];
            int y = trees[i][j+1];
            ans[x][y] = ans[y][x] = 1;
        }
    }
    vector<vector<int>> comps(n);
    for (int i=0; i<n; ++i) {
        if (!trees[i].empty()) {
            comps[dsu.find(trees[i][0])].push_back(trees[i][0]);
        }
    }
    for (int i=0; i<n; ++i) {
        // for (int x: comps[i]) cout << x << ' ';
        // cout << endl;
        if (comps[i].size() == 2) return 0;
        for (int j=0; j+1<comps[i].size(); ++j) {
            int x = comps[i][j];
            int y = comps[i][j+1];
            ans[x][y] = ans[y][x] = 1;
        }
        if (comps[i].size() >= 3) ans[comps[i][0]][comps[i].back()] = ans[comps[i].back()][comps[i][0]] = 1;
    }
	build(ans);
	return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:63:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for (int j=0; j+1<trees[i].size(); ++j) {
      |                       ~~~^~~~~~~~~~~~~~~~
supertrees.cpp:79:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         for (int j=0; j+1<comps[i].size(); ++j) {
      |                       ~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 8 ms 1108 KB Output is correct
7 Correct 198 ms 22020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 8 ms 1108 KB Output is correct
7 Correct 198 ms 22020 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 9 ms 1100 KB Output is correct
13 Correct 166 ms 22068 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 88 ms 8068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 56 ms 5780 KB Output is correct
21 Correct 170 ms 22024 KB Output is correct
22 Correct 172 ms 22020 KB Output is correct
23 Correct 198 ms 22024 KB Output is correct
24 Correct 172 ms 22152 KB Output is correct
25 Correct 70 ms 8196 KB Output is correct
26 Correct 66 ms 8140 KB Output is correct
27 Correct 212 ms 22036 KB Output is correct
28 Correct 172 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 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 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1252 KB Output is correct
9 Correct 168 ms 24124 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 191 ms 24048 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 100 ms 10104 KB Output is correct
18 Correct 0 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 48 ms 6188 KB Output is correct
22 Correct 187 ms 24056 KB Output is correct
23 Correct 169 ms 24012 KB Output is correct
24 Correct 197 ms 24052 KB Output is correct
25 Correct 73 ms 14196 KB Output is correct
26 Correct 73 ms 10112 KB Output is correct
27 Correct 206 ms 24012 KB Output is correct
28 Correct 195 ms 24052 KB Output is correct
29 Correct 82 ms 14192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 44 ms 5800 KB Output is correct
5 Correct 179 ms 22072 KB Output is correct
6 Correct 175 ms 22012 KB Output is correct
7 Correct 203 ms 22024 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 43 ms 5708 KB Output is correct
10 Correct 170 ms 22144 KB Output is correct
11 Correct 167 ms 22056 KB Output is correct
12 Correct 206 ms 22132 KB Output is correct
13 Correct 0 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 44 ms 5880 KB Output is correct
17 Correct 191 ms 22036 KB Output is correct
18 Correct 178 ms 22056 KB Output is correct
19 Correct 185 ms 22104 KB Output is correct
20 Correct 169 ms 22128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 8 ms 1108 KB Output is correct
7 Correct 198 ms 22020 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 9 ms 1100 KB Output is correct
13 Correct 166 ms 22068 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 88 ms 8068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 56 ms 5780 KB Output is correct
21 Correct 170 ms 22024 KB Output is correct
22 Correct 172 ms 22020 KB Output is correct
23 Correct 198 ms 22024 KB Output is correct
24 Correct 172 ms 22152 KB Output is correct
25 Correct 70 ms 8196 KB Output is correct
26 Correct 66 ms 8140 KB Output is correct
27 Correct 212 ms 22036 KB Output is correct
28 Correct 172 ms 22124 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 300 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1252 KB Output is correct
37 Correct 168 ms 24124 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 300 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 191 ms 24048 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 296 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 100 ms 10104 KB Output is correct
46 Correct 0 ms 296 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 48 ms 6188 KB Output is correct
50 Correct 187 ms 24056 KB Output is correct
51 Correct 169 ms 24012 KB Output is correct
52 Correct 197 ms 24052 KB Output is correct
53 Correct 73 ms 14196 KB Output is correct
54 Correct 73 ms 10112 KB Output is correct
55 Correct 206 ms 24012 KB Output is correct
56 Correct 195 ms 24052 KB Output is correct
57 Correct 82 ms 14192 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 5 ms 696 KB Output is correct
61 Correct 94 ms 10112 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 292 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 44 ms 6264 KB Output is correct
66 Correct 71 ms 10112 KB Output is correct
67 Correct 79 ms 10100 KB Output is correct
68 Correct 70 ms 10112 KB Output is correct
69 Correct 75 ms 14208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 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 8 ms 1108 KB Output is correct
7 Correct 198 ms 22020 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 9 ms 1100 KB Output is correct
13 Correct 166 ms 22068 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 596 KB Output is correct
17 Correct 88 ms 8068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 56 ms 5780 KB Output is correct
21 Correct 170 ms 22024 KB Output is correct
22 Correct 172 ms 22020 KB Output is correct
23 Correct 198 ms 22024 KB Output is correct
24 Correct 172 ms 22152 KB Output is correct
25 Correct 70 ms 8196 KB Output is correct
26 Correct 66 ms 8140 KB Output is correct
27 Correct 212 ms 22036 KB Output is correct
28 Correct 172 ms 22124 KB Output is correct
29 Correct 1 ms 296 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 300 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1252 KB Output is correct
37 Correct 168 ms 24124 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 300 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 191 ms 24048 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 296 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 100 ms 10104 KB Output is correct
46 Correct 0 ms 296 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 48 ms 6188 KB Output is correct
50 Correct 187 ms 24056 KB Output is correct
51 Correct 169 ms 24012 KB Output is correct
52 Correct 197 ms 24052 KB Output is correct
53 Correct 73 ms 14196 KB Output is correct
54 Correct 73 ms 10112 KB Output is correct
55 Correct 206 ms 24012 KB Output is correct
56 Correct 195 ms 24052 KB Output is correct
57 Correct 82 ms 14192 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 44 ms 5800 KB Output is correct
62 Correct 179 ms 22072 KB Output is correct
63 Correct 175 ms 22012 KB Output is correct
64 Correct 203 ms 22024 KB Output is correct
65 Correct 0 ms 212 KB Output is correct
66 Correct 43 ms 5708 KB Output is correct
67 Correct 170 ms 22144 KB Output is correct
68 Correct 167 ms 22056 KB Output is correct
69 Correct 206 ms 22132 KB Output is correct
70 Correct 0 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 44 ms 5880 KB Output is correct
74 Correct 191 ms 22036 KB Output is correct
75 Correct 178 ms 22056 KB Output is correct
76 Correct 185 ms 22104 KB Output is correct
77 Correct 169 ms 22128 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 5 ms 696 KB Output is correct
81 Correct 94 ms 10112 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 292 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 44 ms 6264 KB Output is correct
86 Correct 71 ms 10112 KB Output is correct
87 Correct 79 ms 10100 KB Output is correct
88 Correct 70 ms 10112 KB Output is correct
89 Correct 75 ms 14208 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 304 KB Output is correct
92 Correct 4 ms 700 KB Output is correct
93 Correct 87 ms 10104 KB Output is correct
94 Correct 1 ms 296 KB Output is correct
95 Correct 0 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 17 ms 2680 KB Output is correct
98 Correct 70 ms 10100 KB Output is correct
99 Correct 70 ms 10024 KB Output is correct
100 Correct 65 ms 10112 KB Output is correct
101 Correct 67 ms 10112 KB Output is correct
102 Correct 83 ms 10100 KB Output is correct
103 Correct 85 ms 10112 KB Output is correct
104 Correct 80 ms 10060 KB Output is correct
105 Correct 82 ms 10024 KB Output is correct