Submission #377868

# Submission time Handle Problem Language Result Execution time Memory
377868 2021-03-15T11:26:22 Z KoD Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
338 ms 26108 KB
#include "supertrees.h"
#include <bits/stdc++.h>

template <class T>
using Vec = std::vector<T>;

struct DSU {
	Vec<int> par;
	DSU(const int n): par(n, -1) { }
	int find(const int u) {
		return par[u] < 0 ? u : par[u] = find(par[u]);
	}
	void merge(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return;
		if (par[x] > par[y]) std::swap(x, y);
		par[x] += par[y];
		par[y] = x;
	}
};

int construct(Vec<Vec<int>> p) {
	const auto n = (int) p.size();
	std::array<Vec<std::pair<int, int>>, 3> pair;
	for (int i = 0; i < n; ++i) {
		for (int j = i + 1; j < n; ++j) {
			if (p[i][j] == 3) {
				return 0;
			}
			pair[p[i][j]].emplace_back(i, j);
		}
	}
	DSU all(n), one(n);
	for (int k = 1; k <= 2; ++k) {
		for (const auto [x, y]: pair[k]) {
			all.merge(x, y);
		}
	}
	for (const auto [x, y]: pair[1]) {
		one.merge(x, y);
	}
	for (const auto [x, y]: pair[0]) {
		if (all.find(x) == all.find(y)) {
			return 0;
		}
	}
	for (const auto [x, y]: pair[2]) {
		if (one.find(x) == one.find(y)) {
			return 0;
		}
	}
	Vec<Vec<int>> ret(n, Vec<int>(n));
	const auto add_edge = [&](const int x, const int y) {
		ret[x][y] = ret[y][x] = 1;
	};
	for (int i = 0; i < n; ++i) {
		const auto j = one.find(i);
		if (i != j) {
			add_edge(i, j);
		}
	}
	std::map<int, std::set<int>> cycle;
	for (const auto [x, y]: pair[2]) {
		cycle[all.find(x)].insert(one.find(x));
		cycle[all.find(y)].insert(one.find(y));
	}
	for (const auto [_, s]: cycle) {
		Vec<int> v(s.begin(), s.end());
		const auto k = (int) v.size();
		if (k == 1 || k == 2) {
			return 0;
		}
		for (int i = 0; i < k; ++i) {
			add_edge(v[i], v[(i + 1) % k]);
		}
	}
	build(ret);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1384 KB Output is correct
7 Correct 252 ms 24284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1384 KB Output is correct
7 Correct 252 ms 24284 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1384 KB Output is correct
13 Correct 254 ms 24284 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 112 ms 12604 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 69 ms 6624 KB Output is correct
21 Correct 285 ms 24420 KB Output is correct
22 Correct 266 ms 24284 KB Output is correct
23 Correct 262 ms 24400 KB Output is correct
24 Correct 256 ms 24412 KB Output is correct
25 Correct 118 ms 13312 KB Output is correct
26 Correct 112 ms 12768 KB Output is correct
27 Correct 258 ms 24272 KB Output is correct
28 Correct 265 ms 24284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 11 ms 1384 KB Output is correct
9 Correct 256 ms 24284 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 12 ms 1384 KB Output is correct
13 Correct 338 ms 24408 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 112 ms 12476 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 73 ms 6624 KB Output is correct
22 Correct 268 ms 25980 KB Output is correct
23 Correct 260 ms 26096 KB Output is correct
24 Correct 282 ms 26064 KB Output is correct
25 Correct 112 ms 18012 KB Output is correct
26 Correct 114 ms 14868 KB Output is correct
27 Correct 262 ms 26076 KB Output is correct
28 Correct 279 ms 26064 KB Output is correct
29 Correct 113 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 66 ms 6624 KB Output is correct
5 Correct 262 ms 24316 KB Output is correct
6 Correct 258 ms 24284 KB Output is correct
7 Correct 272 ms 24272 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 67 ms 6624 KB Output is correct
10 Correct 266 ms 25980 KB Output is correct
11 Correct 258 ms 26076 KB Output is correct
12 Correct 277 ms 26064 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 66 ms 6752 KB Output is correct
17 Correct 266 ms 26108 KB Output is correct
18 Correct 262 ms 25952 KB Output is correct
19 Correct 261 ms 26084 KB Output is correct
20 Correct 254 ms 26080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1384 KB Output is correct
7 Correct 252 ms 24284 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1384 KB Output is correct
13 Correct 254 ms 24284 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 112 ms 12604 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 69 ms 6624 KB Output is correct
21 Correct 285 ms 24420 KB Output is correct
22 Correct 266 ms 24284 KB Output is correct
23 Correct 262 ms 24400 KB Output is correct
24 Correct 256 ms 24412 KB Output is correct
25 Correct 118 ms 13312 KB Output is correct
26 Correct 112 ms 12768 KB Output is correct
27 Correct 258 ms 24272 KB Output is correct
28 Correct 265 ms 24284 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1384 KB Output is correct
37 Correct 256 ms 24284 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 12 ms 1384 KB Output is correct
41 Correct 338 ms 24408 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 1004 KB Output is correct
45 Correct 112 ms 12476 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 73 ms 6624 KB Output is correct
50 Correct 268 ms 25980 KB Output is correct
51 Correct 260 ms 26096 KB Output is correct
52 Correct 282 ms 26064 KB Output is correct
53 Correct 112 ms 18012 KB Output is correct
54 Correct 114 ms 14868 KB Output is correct
55 Correct 262 ms 26076 KB Output is correct
56 Correct 279 ms 26064 KB Output is correct
57 Correct 113 ms 18012 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 6 ms 1004 KB Output is correct
61 Correct 136 ms 14384 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 67 ms 6752 KB Output is correct
66 Correct 110 ms 14848 KB Output is correct
67 Correct 115 ms 14720 KB Output is correct
68 Correct 108 ms 14440 KB Output is correct
69 Correct 131 ms 18012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 11 ms 1384 KB Output is correct
7 Correct 252 ms 24284 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1384 KB Output is correct
13 Correct 254 ms 24284 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 1004 KB Output is correct
17 Correct 112 ms 12604 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 69 ms 6624 KB Output is correct
21 Correct 285 ms 24420 KB Output is correct
22 Correct 266 ms 24284 KB Output is correct
23 Correct 262 ms 24400 KB Output is correct
24 Correct 256 ms 24412 KB Output is correct
25 Correct 118 ms 13312 KB Output is correct
26 Correct 112 ms 12768 KB Output is correct
27 Correct 258 ms 24272 KB Output is correct
28 Correct 265 ms 24284 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1384 KB Output is correct
37 Correct 256 ms 24284 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 12 ms 1384 KB Output is correct
41 Correct 338 ms 24408 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 1004 KB Output is correct
45 Correct 112 ms 12476 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 1 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 73 ms 6624 KB Output is correct
50 Correct 268 ms 25980 KB Output is correct
51 Correct 260 ms 26096 KB Output is correct
52 Correct 282 ms 26064 KB Output is correct
53 Correct 112 ms 18012 KB Output is correct
54 Correct 114 ms 14868 KB Output is correct
55 Correct 262 ms 26076 KB Output is correct
56 Correct 279 ms 26064 KB Output is correct
57 Correct 113 ms 18012 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 66 ms 6624 KB Output is correct
62 Correct 262 ms 24316 KB Output is correct
63 Correct 258 ms 24284 KB Output is correct
64 Correct 272 ms 24272 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 67 ms 6624 KB Output is correct
67 Correct 266 ms 25980 KB Output is correct
68 Correct 258 ms 26076 KB Output is correct
69 Correct 277 ms 26064 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 66 ms 6752 KB Output is correct
74 Correct 266 ms 26108 KB Output is correct
75 Correct 262 ms 25952 KB Output is correct
76 Correct 261 ms 26084 KB Output is correct
77 Correct 254 ms 26080 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 1 ms 364 KB Output is correct
80 Correct 6 ms 1004 KB Output is correct
81 Correct 136 ms 14384 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 67 ms 6752 KB Output is correct
86 Correct 110 ms 14848 KB Output is correct
87 Correct 115 ms 14720 KB Output is correct
88 Correct 108 ms 14440 KB Output is correct
89 Correct 131 ms 18012 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 4 ms 748 KB Output is correct
93 Correct 97 ms 10220 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 26 ms 3180 KB Output is correct
98 Correct 99 ms 10892 KB Output is correct
99 Correct 103 ms 11400 KB Output is correct
100 Correct 100 ms 11504 KB Output is correct
101 Correct 106 ms 14432 KB Output is correct
102 Correct 119 ms 10220 KB Output is correct
103 Correct 92 ms 10192 KB Output is correct
104 Correct 90 ms 10220 KB Output is correct
105 Correct 105 ms 10220 KB Output is correct