Submission #651125

# Submission time Handle Problem Language Result Execution time Memory
651125 2022-10-17T09:19:18 Z Clan328 Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
211 ms 24264 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>
 
using namespace std;
 
vector<int> m_link, sz;
 
int m_find(int x) {
	if (m_link[x] != x) m_link[x] = m_find(m_link[x]);
	return m_link[x];
}
 
bool same(int a, int b) {
	return m_find(a) == m_find(b);
}
 
void unite(int a, int b) {
	if (a > b) swap(a, b);
	int x = m_find(b), y = m_find(a);
	if (same(x, y)) return;
	sz[y] += sz[x];
	m_link[x] = y;
}
 
int construct(vector<vector<int>> p) {
	int n = p.size();
	vector<vector<int>> answer(n, vector<int>(n));
 
	m_link = vector<int>(n);
	sz = vector<int>(n, 1);
	iota(m_link.begin(), m_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 = m_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 = m_find(i), p2 = m_find(j);
				unite(p1, p2);
				newNodes[m_find(p1)].insert(p1);
				newNodes[m_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 1208 KB Output is correct
7 Correct 184 ms 24140 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 1208 KB Output is correct
7 Correct 184 ms 24140 KB Output is correct
8 Correct 1 ms 296 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 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 171 ms 24048 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 796 KB Output is correct
17 Correct 91 ms 14028 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 44 ms 6260 KB Output is correct
21 Correct 191 ms 23936 KB Output is correct
22 Correct 182 ms 24012 KB Output is correct
23 Correct 211 ms 24060 KB Output is correct
24 Correct 188 ms 24124 KB Output is correct
25 Correct 113 ms 14068 KB Output is correct
26 Correct 92 ms 14080 KB Output is correct
27 Correct 202 ms 24036 KB Output is correct
28 Correct 180 ms 24264 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 300 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1236 KB Output is correct
9 Correct 186 ms 24048 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 9 ms 1264 KB Output is correct
13 Correct 210 ms 24064 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 852 KB Output is correct
17 Correct 106 ms 14196 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 50 ms 6244 KB Output is correct
22 Correct 174 ms 24188 KB Output is correct
23 Correct 191 ms 24012 KB Output is correct
24 Correct 197 ms 24052 KB Output is correct
25 Correct 99 ms 14028 KB Output is correct
26 Correct 77 ms 14080 KB Output is correct
27 Correct 171 ms 24052 KB Output is correct
28 Correct 197 ms 24052 KB Output is correct
29 Correct 69 ms 14028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 47 ms 6228 KB Output is correct
5 Correct 180 ms 23972 KB Output is correct
6 Correct 192 ms 23932 KB Output is correct
7 Correct 191 ms 24160 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 44 ms 6256 KB Output is correct
10 Correct 183 ms 24064 KB Output is correct
11 Correct 180 ms 24012 KB Output is correct
12 Correct 202 ms 24012 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 45 ms 6260 KB Output is correct
17 Correct 174 ms 24024 KB Output is correct
18 Correct 177 ms 24004 KB Output is correct
19 Correct 172 ms 24184 KB Output is correct
20 Correct 167 ms 24024 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 1208 KB Output is correct
7 Correct 184 ms 24140 KB Output is correct
8 Correct 1 ms 296 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 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 171 ms 24048 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 796 KB Output is correct
17 Correct 91 ms 14028 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 44 ms 6260 KB Output is correct
21 Correct 191 ms 23936 KB Output is correct
22 Correct 182 ms 24012 KB Output is correct
23 Correct 211 ms 24060 KB Output is correct
24 Correct 188 ms 24124 KB Output is correct
25 Correct 113 ms 14068 KB Output is correct
26 Correct 92 ms 14080 KB Output is correct
27 Correct 202 ms 24036 KB Output is correct
28 Correct 180 ms 24264 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 300 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1236 KB Output is correct
37 Correct 186 ms 24048 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1264 KB Output is correct
41 Correct 210 ms 24064 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 106 ms 14196 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 50 ms 6244 KB Output is correct
50 Correct 174 ms 24188 KB Output is correct
51 Correct 191 ms 24012 KB Output is correct
52 Correct 197 ms 24052 KB Output is correct
53 Correct 99 ms 14028 KB Output is correct
54 Correct 77 ms 14080 KB Output is correct
55 Correct 171 ms 24052 KB Output is correct
56 Correct 197 ms 24052 KB Output is correct
57 Correct 69 ms 14028 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 292 KB Output is correct
60 Correct 4 ms 824 KB Output is correct
61 Correct 100 ms 14064 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 45 ms 6288 KB Output is correct
66 Correct 79 ms 14092 KB Output is correct
67 Correct 78 ms 14080 KB Output is correct
68 Correct 84 ms 14068 KB Output is correct
69 Correct 71 ms 14084 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 1208 KB Output is correct
7 Correct 184 ms 24140 KB Output is correct
8 Correct 1 ms 296 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 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 171 ms 24048 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 796 KB Output is correct
17 Correct 91 ms 14028 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 44 ms 6260 KB Output is correct
21 Correct 191 ms 23936 KB Output is correct
22 Correct 182 ms 24012 KB Output is correct
23 Correct 211 ms 24060 KB Output is correct
24 Correct 188 ms 24124 KB Output is correct
25 Correct 113 ms 14068 KB Output is correct
26 Correct 92 ms 14080 KB Output is correct
27 Correct 202 ms 24036 KB Output is correct
28 Correct 180 ms 24264 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 300 KB Output is correct
34 Correct 1 ms 300 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1236 KB Output is correct
37 Correct 186 ms 24048 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 9 ms 1264 KB Output is correct
41 Correct 210 ms 24064 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 5 ms 852 KB Output is correct
45 Correct 106 ms 14196 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 50 ms 6244 KB Output is correct
50 Correct 174 ms 24188 KB Output is correct
51 Correct 191 ms 24012 KB Output is correct
52 Correct 197 ms 24052 KB Output is correct
53 Correct 99 ms 14028 KB Output is correct
54 Correct 77 ms 14080 KB Output is correct
55 Correct 171 ms 24052 KB Output is correct
56 Correct 197 ms 24052 KB Output is correct
57 Correct 69 ms 14028 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 296 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 47 ms 6228 KB Output is correct
62 Correct 180 ms 23972 KB Output is correct
63 Correct 192 ms 23932 KB Output is correct
64 Correct 191 ms 24160 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 44 ms 6256 KB Output is correct
67 Correct 183 ms 24064 KB Output is correct
68 Correct 180 ms 24012 KB Output is correct
69 Correct 202 ms 24012 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 300 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 45 ms 6260 KB Output is correct
74 Correct 174 ms 24024 KB Output is correct
75 Correct 177 ms 24004 KB Output is correct
76 Correct 172 ms 24184 KB Output is correct
77 Correct 167 ms 24024 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 292 KB Output is correct
80 Correct 4 ms 824 KB Output is correct
81 Correct 100 ms 14064 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 45 ms 6288 KB Output is correct
86 Correct 79 ms 14092 KB Output is correct
87 Correct 78 ms 14080 KB Output is correct
88 Correct 84 ms 14068 KB Output is correct
89 Correct 71 ms 14084 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 93 ms 14124 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 24 ms 3708 KB Output is correct
98 Correct 77 ms 14064 KB Output is correct
99 Correct 77 ms 14064 KB Output is correct
100 Correct 76 ms 14072 KB Output is correct
101 Correct 76 ms 14120 KB Output is correct
102 Correct 71 ms 14068 KB Output is correct
103 Correct 93 ms 14072 KB Output is correct
104 Correct 86 ms 14076 KB Output is correct
105 Correct 81 ms 14064 KB Output is correct