Submission #779417

# Submission time Handle Problem Language Result Execution time Memory
779417 2023-07-11T11:40:59 Z Josia Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
226 ms 24276 KB
#include "supertrees.h"
#include <vector>
#include <bits/stdc++.h>

using namespace std;




struct uf {
	vector<int> chefs;
	uf (int n) {
		chefs.resize(n);
		for (int i = 0; i<n; i++) chefs[i] = i;
	}

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

	void unite(int a, int b) {
		a = find(a);
		b = find(b);

		chefs[a] = b;
	}
};





int construct(std::vector<std::vector<int>> p) {
	for (auto i: p) {
		for (auto j: i) if (j == 3) return 0;
	}

	int n = p.size();
	if (n == 1) {
		if (p[0][0] == 1) {
			build({{0}});
			return 1;
		}
		return 0;
	}

	// ----
	uf u(n);

	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			if (p[i][j]) u.unite(i, j);
		}
	}

	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			if ((bool)(p[i][j]) != (u.find(i) == u.find(j))) return 0;
		}
	}

	vector<vector<int>> vals(n);

	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			if (u.find(j) == i) vals[i].push_back(j);
		}
	}
	// ----


	uf u2(n);

	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			if (p[i][j]==1) u2.unite(i, j);
		}
	}

	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			if ((p[i][j]==1) != (u2.find(i) == u2.find(j))) return 0;
		}
	}

	vector<vector<int>> vals2(n);

	for (int i = 0; i<n; i++) {
		for (int j = 0; j<n; j++) {
			if (u2.find(j) == i) vals2[i].push_back(j);
		}
	}



	vector<vector<int>> secondConnect(n);

	vector<vector<int>> answer(n, vector<int>(n));

	for (auto v: vals2) {
		if (v.size()) secondConnect[u.find(v[0])].push_back(u2.find(v[0]));
		for (int i = 0; i<(int)(v.size())-1; i++) {
			answer[v[i]][v[(i+1)%v.size()]] = 1;
			answer[v[(i+1)%v.size()]][v[i]] = 1;
		}
	}

	for (auto v: secondConnect) {
		if (v.size() == 1) continue;
		if (v.size() == 2) return 0;
 
		for (int i = 0; i<(int)(v.size()); i++) {
			answer[v[i]][v[(i+1)%v.size()]] = 1;
			answer[v[(i+1)%v.size()]][v[i]] = 1;
		}
 
	}

	build(answer);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1236 KB Output is correct
7 Correct 181 ms 22384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1236 KB Output is correct
7 Correct 181 ms 22384 KB Output is correct
8 Correct 1 ms 352 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 10 ms 1268 KB Output is correct
13 Correct 215 ms 22456 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 97 ms 8320 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 41 ms 5960 KB Output is correct
21 Correct 172 ms 22340 KB Output is correct
22 Correct 173 ms 22300 KB Output is correct
23 Correct 186 ms 22304 KB Output is correct
24 Correct 224 ms 22380 KB Output is correct
25 Correct 76 ms 8336 KB Output is correct
26 Correct 72 ms 8336 KB Output is correct
27 Correct 197 ms 22304 KB Output is correct
28 Correct 171 ms 22360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 0 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 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 7 ms 1236 KB Output is correct
9 Correct 174 ms 24276 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 11 ms 1208 KB Output is correct
13 Correct 200 ms 24088 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 668 KB Output is correct
17 Correct 71 ms 10100 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 296 KB Output is correct
21 Correct 40 ms 6312 KB Output is correct
22 Correct 186 ms 24028 KB Output is correct
23 Correct 194 ms 24052 KB Output is correct
24 Correct 178 ms 24116 KB Output is correct
25 Correct 81 ms 14196 KB Output is correct
26 Correct 62 ms 10096 KB Output is correct
27 Correct 173 ms 24012 KB Output is correct
28 Correct 186 ms 24016 KB Output is correct
29 Correct 68 ms 14212 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 42 ms 6188 KB Output is correct
5 Correct 168 ms 22564 KB Output is correct
6 Correct 171 ms 22604 KB Output is correct
7 Correct 173 ms 22660 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 41 ms 6256 KB Output is correct
10 Correct 164 ms 22656 KB Output is correct
11 Correct 171 ms 22604 KB Output is correct
12 Correct 166 ms 22668 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 276 KB Output is correct
15 Correct 1 ms 300 KB Output is correct
16 Correct 44 ms 6232 KB Output is correct
17 Correct 176 ms 24124 KB Output is correct
18 Correct 226 ms 24012 KB Output is correct
19 Correct 165 ms 24052 KB Output is correct
20 Correct 179 ms 24052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1236 KB Output is correct
7 Correct 181 ms 22384 KB Output is correct
8 Correct 1 ms 352 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 10 ms 1268 KB Output is correct
13 Correct 215 ms 22456 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 97 ms 8320 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 41 ms 5960 KB Output is correct
21 Correct 172 ms 22340 KB Output is correct
22 Correct 173 ms 22300 KB Output is correct
23 Correct 186 ms 22304 KB Output is correct
24 Correct 224 ms 22380 KB Output is correct
25 Correct 76 ms 8336 KB Output is correct
26 Correct 72 ms 8336 KB Output is correct
27 Correct 197 ms 22304 KB Output is correct
28 Correct 171 ms 22360 KB Output is correct
29 Correct 1 ms 300 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 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 296 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 174 ms 24276 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 11 ms 1208 KB Output is correct
41 Correct 200 ms 24088 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 668 KB Output is correct
45 Correct 71 ms 10100 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 296 KB Output is correct
49 Correct 40 ms 6312 KB Output is correct
50 Correct 186 ms 24028 KB Output is correct
51 Correct 194 ms 24052 KB Output is correct
52 Correct 178 ms 24116 KB Output is correct
53 Correct 81 ms 14196 KB Output is correct
54 Correct 62 ms 10096 KB Output is correct
55 Correct 173 ms 24012 KB Output is correct
56 Correct 186 ms 24016 KB Output is correct
57 Correct 68 ms 14212 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 300 KB Output is correct
60 Correct 3 ms 668 KB Output is correct
61 Correct 69 ms 10096 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 6220 KB Output is correct
66 Correct 64 ms 10096 KB Output is correct
67 Correct 62 ms 10112 KB Output is correct
68 Correct 73 ms 10116 KB Output is correct
69 Correct 75 ms 14196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 1236 KB Output is correct
7 Correct 181 ms 22384 KB Output is correct
8 Correct 1 ms 352 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 10 ms 1268 KB Output is correct
13 Correct 215 ms 22456 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 97 ms 8320 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 41 ms 5960 KB Output is correct
21 Correct 172 ms 22340 KB Output is correct
22 Correct 173 ms 22300 KB Output is correct
23 Correct 186 ms 22304 KB Output is correct
24 Correct 224 ms 22380 KB Output is correct
25 Correct 76 ms 8336 KB Output is correct
26 Correct 72 ms 8336 KB Output is correct
27 Correct 197 ms 22304 KB Output is correct
28 Correct 171 ms 22360 KB Output is correct
29 Correct 1 ms 300 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 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 296 KB Output is correct
36 Correct 7 ms 1236 KB Output is correct
37 Correct 174 ms 24276 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 11 ms 1208 KB Output is correct
41 Correct 200 ms 24088 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 668 KB Output is correct
45 Correct 71 ms 10100 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 0 ms 296 KB Output is correct
49 Correct 40 ms 6312 KB Output is correct
50 Correct 186 ms 24028 KB Output is correct
51 Correct 194 ms 24052 KB Output is correct
52 Correct 178 ms 24116 KB Output is correct
53 Correct 81 ms 14196 KB Output is correct
54 Correct 62 ms 10096 KB Output is correct
55 Correct 173 ms 24012 KB Output is correct
56 Correct 186 ms 24016 KB Output is correct
57 Correct 68 ms 14212 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 42 ms 6188 KB Output is correct
62 Correct 168 ms 22564 KB Output is correct
63 Correct 171 ms 22604 KB Output is correct
64 Correct 173 ms 22660 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 41 ms 6256 KB Output is correct
67 Correct 164 ms 22656 KB Output is correct
68 Correct 171 ms 22604 KB Output is correct
69 Correct 166 ms 22668 KB Output is correct
70 Correct 0 ms 296 KB Output is correct
71 Correct 1 ms 276 KB Output is correct
72 Correct 1 ms 300 KB Output is correct
73 Correct 44 ms 6232 KB Output is correct
74 Correct 176 ms 24124 KB Output is correct
75 Correct 226 ms 24012 KB Output is correct
76 Correct 165 ms 24052 KB Output is correct
77 Correct 179 ms 24052 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 300 KB Output is correct
80 Correct 3 ms 668 KB Output is correct
81 Correct 69 ms 10096 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 6220 KB Output is correct
86 Correct 64 ms 10096 KB Output is correct
87 Correct 62 ms 10112 KB Output is correct
88 Correct 73 ms 10116 KB Output is correct
89 Correct 75 ms 14196 KB Output is correct
90 Correct 0 ms 296 KB Output is correct
91 Correct 1 ms 300 KB Output is correct
92 Correct 3 ms 696 KB Output is correct
93 Correct 77 ms 10100 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 16 ms 2680 KB Output is correct
98 Correct 73 ms 10096 KB Output is correct
99 Correct 61 ms 10104 KB Output is correct
100 Correct 75 ms 10024 KB Output is correct
101 Correct 60 ms 10104 KB Output is correct
102 Correct 66 ms 10100 KB Output is correct
103 Correct 62 ms 10060 KB Output is correct
104 Correct 86 ms 10112 KB Output is correct
105 Correct 64 ms 10028 KB Output is correct