Submission #579707

# Submission time Handle Problem Language Result Execution time Memory
579707 2022-06-19T16:17:34 Z Clan328 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
245 ms 24088 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>

using namespace std;

vector<int> link, sz;

int find(int x) {
	if (link[x] != x) link[x] = find(link[x]);
	return link[x];
}

bool same(int a, int b) {
	return find(a) == find(b);
}

void unite(int a, int b) {
	if (a > b) swap(a, b);
	int x = find(b), y = find(a);
	if (same(x, y)) return;
	sz[y] += sz[x];
	link[x] = y;
}

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

	link = vector<int>(n);
	sz = vector<int>(n, 1);
	iota(link.begin(), link.end(), 0);

	bool res = true;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i == j) res &= p[i][j] == 1;
			else if (p[i][j] == 1) unite(i, j);
			else if (p[i][j] == 3) res = false;
		}
	}

	for (int i = 0; i < n; i++) {
		int par = find(i);
		if (par != i) {
			answer[par][i] = 1;
			answer[i][par] = 1;

			for (int j = 0; j < n; j++) {
				res &= p[i][j] == p[par][j];
			}
		}
	}

	vector<set<int>> newNodes(n);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i == j) continue;
			if (p[i][j] == 2) {
				int p1 = find(i), p2 = find(j);
				unite(p1, p2);
				newNodes[find(p1)].insert(p1);
				newNodes[find(p2)].insert(p2);
			}
		}
	}

	for (int i = 0; i < n; i++) {
		if (newNodes[i].size() == 0) continue;
		res &= newNodes[i].size() > 2;

		for (auto it = newNodes[i].begin(); it != newNodes[i].end(); it++) {
			auto nxt = it;
			nxt++;
			
			if (nxt == newNodes[i].end()) {
				nxt = newNodes[i].begin();
			}
			answer[*it][*nxt] = 1;
			answer[*nxt][*it] = 1;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (i == j) continue;
			res &= ((p[i][j] > 0) == same(i, j));
		}
	}

	if (res) {
		build(answer);
		return 1;
	} else return 0;
}
# 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 188 ms 22308 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 188 ms 22308 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 7 ms 1180 KB Output is correct
13 Correct 182 ms 22020 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 97 ms 12164 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 42 ms 5764 KB Output is correct
21 Correct 196 ms 22016 KB Output is correct
22 Correct 176 ms 22092 KB Output is correct
23 Correct 203 ms 22076 KB Output is correct
24 Correct 174 ms 22020 KB Output is correct
25 Correct 76 ms 12168 KB Output is correct
26 Correct 74 ms 12168 KB Output is correct
27 Correct 200 ms 22060 KB Output is correct
28 Correct 190 ms 22020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1108 KB Output is correct
9 Correct 169 ms 22060 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 10 ms 1188 KB Output is correct
13 Correct 223 ms 22148 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 828 KB Output is correct
17 Correct 116 ms 14132 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 52 ms 6260 KB Output is correct
22 Correct 180 ms 24012 KB Output is correct
23 Correct 208 ms 24064 KB Output is correct
24 Correct 208 ms 24088 KB Output is correct
25 Correct 79 ms 14076 KB Output is correct
26 Correct 80 ms 14084 KB Output is correct
27 Correct 178 ms 24052 KB Output is correct
28 Correct 219 ms 24072 KB Output is correct
29 Correct 75 ms 14068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 47 ms 5680 KB Output is correct
5 Correct 186 ms 21980 KB Output is correct
6 Correct 189 ms 22080 KB Output is correct
7 Correct 194 ms 22016 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 54 ms 5764 KB Output is correct
10 Correct 173 ms 22124 KB Output is correct
11 Correct 185 ms 22148 KB Output is correct
12 Correct 202 ms 22096 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 55 ms 5788 KB Output is correct
17 Correct 177 ms 22152 KB Output is correct
18 Correct 245 ms 22028 KB Output is correct
19 Correct 174 ms 22012 KB Output is correct
20 Correct 187 ms 22028 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 188 ms 22308 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 7 ms 1180 KB Output is correct
13 Correct 182 ms 22020 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 97 ms 12164 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 42 ms 5764 KB Output is correct
21 Correct 196 ms 22016 KB Output is correct
22 Correct 176 ms 22092 KB Output is correct
23 Correct 203 ms 22076 KB Output is correct
24 Correct 174 ms 22020 KB Output is correct
25 Correct 76 ms 12168 KB Output is correct
26 Correct 74 ms 12168 KB Output is correct
27 Correct 200 ms 22060 KB Output is correct
28 Correct 190 ms 22020 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1108 KB Output is correct
37 Correct 169 ms 22060 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 10 ms 1188 KB Output is correct
41 Correct 223 ms 22148 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 5 ms 828 KB Output is correct
45 Correct 116 ms 14132 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 52 ms 6260 KB Output is correct
50 Correct 180 ms 24012 KB Output is correct
51 Correct 208 ms 24064 KB Output is correct
52 Correct 208 ms 24088 KB Output is correct
53 Correct 79 ms 14076 KB Output is correct
54 Correct 80 ms 14084 KB Output is correct
55 Correct 178 ms 24052 KB Output is correct
56 Correct 219 ms 24072 KB Output is correct
57 Correct 75 ms 14068 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 852 KB Output is correct
61 Correct 98 ms 14068 KB Output is correct
62 Correct 1 ms 304 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 49 ms 6220 KB Output is correct
66 Correct 85 ms 14068 KB Output is correct
67 Correct 78 ms 14080 KB Output is correct
68 Correct 77 ms 14156 KB Output is correct
69 Correct 76 ms 14120 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 8 ms 1236 KB Output is correct
7 Correct 188 ms 22308 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 7 ms 1180 KB Output is correct
13 Correct 182 ms 22020 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 97 ms 12164 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 42 ms 5764 KB Output is correct
21 Correct 196 ms 22016 KB Output is correct
22 Correct 176 ms 22092 KB Output is correct
23 Correct 203 ms 22076 KB Output is correct
24 Correct 174 ms 22020 KB Output is correct
25 Correct 76 ms 12168 KB Output is correct
26 Correct 74 ms 12168 KB Output is correct
27 Correct 200 ms 22060 KB Output is correct
28 Correct 190 ms 22020 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1108 KB Output is correct
37 Correct 169 ms 22060 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 10 ms 1188 KB Output is correct
41 Correct 223 ms 22148 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 5 ms 828 KB Output is correct
45 Correct 116 ms 14132 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 52 ms 6260 KB Output is correct
50 Correct 180 ms 24012 KB Output is correct
51 Correct 208 ms 24064 KB Output is correct
52 Correct 208 ms 24088 KB Output is correct
53 Correct 79 ms 14076 KB Output is correct
54 Correct 80 ms 14084 KB Output is correct
55 Correct 178 ms 24052 KB Output is correct
56 Correct 219 ms 24072 KB Output is correct
57 Correct 75 ms 14068 KB Output is correct
58 Correct 1 ms 256 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 47 ms 5680 KB Output is correct
62 Correct 186 ms 21980 KB Output is correct
63 Correct 189 ms 22080 KB Output is correct
64 Correct 194 ms 22016 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 54 ms 5764 KB Output is correct
67 Correct 173 ms 22124 KB Output is correct
68 Correct 185 ms 22148 KB Output is correct
69 Correct 202 ms 22096 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 55 ms 5788 KB Output is correct
74 Correct 177 ms 22152 KB Output is correct
75 Correct 245 ms 22028 KB Output is correct
76 Correct 174 ms 22012 KB Output is correct
77 Correct 187 ms 22028 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 852 KB Output is correct
81 Correct 98 ms 14068 KB Output is correct
82 Correct 1 ms 304 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 49 ms 6220 KB Output is correct
86 Correct 85 ms 14068 KB Output is correct
87 Correct 78 ms 14080 KB Output is correct
88 Correct 77 ms 14156 KB Output is correct
89 Correct 76 ms 14120 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 5 ms 852 KB Output is correct
93 Correct 95 ms 14192 KB Output is correct
94 Correct 1 ms 304 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 19 ms 3788 KB Output is correct
98 Correct 79 ms 14124 KB Output is correct
99 Correct 79 ms 14080 KB Output is correct
100 Correct 82 ms 14068 KB Output is correct
101 Correct 74 ms 14028 KB Output is correct
102 Correct 69 ms 14072 KB Output is correct
103 Correct 134 ms 14184 KB Output is correct
104 Correct 88 ms 14028 KB Output is correct
105 Correct 87 ms 14028 KB Output is correct