Submission #612155

# Submission time Handle Problem Language Result Execution time Memory
612155 2022-07-29T11:11:49 Z erekle Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
296 ms 24132 KB
#include "supertrees.h"
#include <vector>

using namespace std;

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;
		}
	}

	// Split into components
	vector<vector<int>> components;
	vector<int> myCmp(n, -1);
	for (int i = 0; i < n; ++i) {
		if (myCmp[i] != -1) continue;
		components.push_back({});
		for (int j = 0; j < n; ++j) {
			if (p[i][j]) {
				if (myCmp[j] != -1) return 0;
				components.back().push_back(j);
				myCmp[j] = (int)components.size()-1;
			}
		}
	}

	// Check components are separate
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if ((myCmp[i] == myCmp[j]) ^ (p[i][j] != 0)) return 0;
		}
	}

	// Build answer here
	vector<vector<int>> edge(n, vector<int>(n));

	// Now solve separately for each component
	for (vector<int> &c : components) {
		// Split into 1-chains
		vector<vector<int>> chains;
		vector<int> myChain(n, -1);
		for (int i : c) {
			if (myChain[i] != -1) continue;
			chains.push_back({});
			for (int j : c) {
				if (p[i][j] == 1) {
					if (myChain[j] != -1) return 0;
					chains.back().push_back(j);
					myChain[j] = (int)chains.size()-1;
				}
			}
		}

		// Check 1-chains are separate
		for (int i : c) {
			for (int j : c) {
				if ((myChain[i] == myChain[j]) ^ (p[i][j] == 1)) return 0;
			}
		}

		// Assign a chain leader to each chain
		int m = chains.size();
		vector<int> chainLeader(m);
		for (int i = 0; i < m; ++i) chainLeader[i] = chains[i][0];
		
		// Check chains and their leaders behave in the same way
		for (int i : c) {
			for (int j : c) {
				if (p[i][j] != p[chainLeader[myChain[i]]][j]) return 0;
			}
		}

		// There will be two ways to travel between a pair of nodes in different chains
		if (m == 2) return 0; // cannot have two chains (cannot make cycle)

		// Connect within chains
		for (int i : c) {
			if (chainLeader[myChain[i]] != i) {
				edge[i][chainLeader[myChain[i]]] = edge[chainLeader[myChain[i]]][i] = 1;
			}
		}

		// Connect chain leaders in a cycle
		if (m == 1) continue; // only one chain - no need
		for (int i = 0; i < m; ++i) {
			int j = (i+1)%m;
			edge[chainLeader[i]][chainLeader[j]] = edge[chainLeader[j]][chainLeader[i]] = 1;
		}
	}

	build(edge);
	return 1;
}
# 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 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 15 ms 1168 KB Output is correct
7 Correct 241 ms 23908 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 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 15 ms 1168 KB Output is correct
7 Correct 241 ms 23908 KB Output is correct
8 Correct 1 ms 288 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 13 ms 1236 KB Output is correct
13 Correct 244 ms 24012 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 688 KB Output is correct
17 Correct 95 ms 10092 KB Output is correct
18 Correct 1 ms 288 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 76 ms 6204 KB Output is correct
21 Correct 222 ms 23940 KB Output is correct
22 Correct 225 ms 23976 KB Output is correct
23 Correct 237 ms 23936 KB Output is correct
24 Correct 216 ms 24040 KB Output is correct
25 Correct 92 ms 10072 KB Output is correct
26 Correct 102 ms 10092 KB Output is correct
27 Correct 265 ms 23880 KB Output is correct
28 Correct 247 ms 24132 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 336 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 0 ms 284 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 8 ms 1236 KB Output is correct
9 Correct 246 ms 24020 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 1204 KB Output is correct
13 Correct 245 ms 23964 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 292 KB Output is correct
16 Correct 6 ms 684 KB Output is correct
17 Correct 102 ms 10120 KB Output is correct
18 Correct 0 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 48 ms 6244 KB Output is correct
22 Correct 188 ms 23912 KB Output is correct
23 Correct 296 ms 23908 KB Output is correct
24 Correct 213 ms 24012 KB Output is correct
25 Correct 111 ms 14056 KB Output is correct
26 Correct 130 ms 10088 KB Output is correct
27 Correct 212 ms 23968 KB Output is correct
28 Correct 273 ms 23932 KB Output is correct
29 Correct 99 ms 14108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 284 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 69 ms 6216 KB Output is correct
5 Correct 274 ms 23912 KB Output is correct
6 Correct 197 ms 24016 KB Output is correct
7 Correct 245 ms 23956 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 50 ms 6240 KB Output is correct
10 Correct 213 ms 23980 KB Output is correct
11 Correct 197 ms 24012 KB Output is correct
12 Correct 258 ms 23992 KB Output is correct
13 Correct 1 ms 260 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 288 KB Output is correct
16 Correct 69 ms 6244 KB Output is correct
17 Correct 257 ms 23940 KB Output is correct
18 Correct 295 ms 23924 KB Output is correct
19 Correct 269 ms 23892 KB Output is correct
20 Correct 278 ms 24048 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 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 15 ms 1168 KB Output is correct
7 Correct 241 ms 23908 KB Output is correct
8 Correct 1 ms 288 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 13 ms 1236 KB Output is correct
13 Correct 244 ms 24012 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 688 KB Output is correct
17 Correct 95 ms 10092 KB Output is correct
18 Correct 1 ms 288 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 76 ms 6204 KB Output is correct
21 Correct 222 ms 23940 KB Output is correct
22 Correct 225 ms 23976 KB Output is correct
23 Correct 237 ms 23936 KB Output is correct
24 Correct 216 ms 24040 KB Output is correct
25 Correct 92 ms 10072 KB Output is correct
26 Correct 102 ms 10092 KB Output is correct
27 Correct 265 ms 23880 KB Output is correct
28 Correct 247 ms 24132 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 336 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 0 ms 284 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1236 KB Output is correct
37 Correct 246 ms 24020 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 1204 KB Output is correct
41 Correct 245 ms 23964 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 292 KB Output is correct
44 Correct 6 ms 684 KB Output is correct
45 Correct 102 ms 10120 KB Output is correct
46 Correct 0 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 48 ms 6244 KB Output is correct
50 Correct 188 ms 23912 KB Output is correct
51 Correct 296 ms 23908 KB Output is correct
52 Correct 213 ms 24012 KB Output is correct
53 Correct 111 ms 14056 KB Output is correct
54 Correct 130 ms 10088 KB Output is correct
55 Correct 212 ms 23968 KB Output is correct
56 Correct 273 ms 23932 KB Output is correct
57 Correct 99 ms 14108 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 5 ms 596 KB Output is correct
61 Correct 125 ms 10012 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 0 ms 212 KB Output is correct
65 Correct 48 ms 6220 KB Output is correct
66 Correct 76 ms 10060 KB Output is correct
67 Correct 81 ms 10084 KB Output is correct
68 Correct 112 ms 10084 KB Output is correct
69 Correct 91 ms 14052 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 292 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 15 ms 1168 KB Output is correct
7 Correct 241 ms 23908 KB Output is correct
8 Correct 1 ms 288 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 13 ms 1236 KB Output is correct
13 Correct 244 ms 24012 KB Output is correct
14 Correct 1 ms 288 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 5 ms 688 KB Output is correct
17 Correct 95 ms 10092 KB Output is correct
18 Correct 1 ms 288 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 76 ms 6204 KB Output is correct
21 Correct 222 ms 23940 KB Output is correct
22 Correct 225 ms 23976 KB Output is correct
23 Correct 237 ms 23936 KB Output is correct
24 Correct 216 ms 24040 KB Output is correct
25 Correct 92 ms 10072 KB Output is correct
26 Correct 102 ms 10092 KB Output is correct
27 Correct 265 ms 23880 KB Output is correct
28 Correct 247 ms 24132 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 336 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 0 ms 284 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 8 ms 1236 KB Output is correct
37 Correct 246 ms 24020 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 1204 KB Output is correct
41 Correct 245 ms 23964 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 1 ms 292 KB Output is correct
44 Correct 6 ms 684 KB Output is correct
45 Correct 102 ms 10120 KB Output is correct
46 Correct 0 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 48 ms 6244 KB Output is correct
50 Correct 188 ms 23912 KB Output is correct
51 Correct 296 ms 23908 KB Output is correct
52 Correct 213 ms 24012 KB Output is correct
53 Correct 111 ms 14056 KB Output is correct
54 Correct 130 ms 10088 KB Output is correct
55 Correct 212 ms 23968 KB Output is correct
56 Correct 273 ms 23932 KB Output is correct
57 Correct 99 ms 14108 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 284 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 69 ms 6216 KB Output is correct
62 Correct 274 ms 23912 KB Output is correct
63 Correct 197 ms 24016 KB Output is correct
64 Correct 245 ms 23956 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 50 ms 6240 KB Output is correct
67 Correct 213 ms 23980 KB Output is correct
68 Correct 197 ms 24012 KB Output is correct
69 Correct 258 ms 23992 KB Output is correct
70 Correct 1 ms 260 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 288 KB Output is correct
73 Correct 69 ms 6244 KB Output is correct
74 Correct 257 ms 23940 KB Output is correct
75 Correct 295 ms 23924 KB Output is correct
76 Correct 269 ms 23892 KB Output is correct
77 Correct 278 ms 24048 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 5 ms 596 KB Output is correct
81 Correct 125 ms 10012 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 0 ms 212 KB Output is correct
85 Correct 48 ms 6220 KB Output is correct
86 Correct 76 ms 10060 KB Output is correct
87 Correct 81 ms 10084 KB Output is correct
88 Correct 112 ms 10084 KB Output is correct
89 Correct 91 ms 14052 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 0 ms 284 KB Output is correct
92 Correct 5 ms 656 KB Output is correct
93 Correct 83 ms 10060 KB Output is correct
94 Correct 1 ms 288 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 21 ms 2664 KB Output is correct
98 Correct 85 ms 10084 KB Output is correct
99 Correct 78 ms 10104 KB Output is correct
100 Correct 80 ms 10096 KB Output is correct
101 Correct 83 ms 10092 KB Output is correct
102 Correct 77 ms 10084 KB Output is correct
103 Correct 80 ms 10092 KB Output is correct
104 Correct 80 ms 10008 KB Output is correct
105 Correct 81 ms 10100 KB Output is correct