Submission #351972

# Submission time Handle Problem Language Result Execution time Memory
351972 2021-01-20T10:16:20 Z BlancaHM Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
300 ms 24172 KB
#include <iostream>
#include <vector>
#include <unordered_map>
#include <fstream>
#include "supertrees.h"
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;

int root(int a, vector<int> & parent) {
	if (a == parent[a])
		return a;
	return parent[a] = root(parent[a], parent);
}

void merge(int i, int j, vector<int> & parent, vector<int> & rnk, int & numCCs) {
	int root_i = root(i, parent), root_j = root(j, parent);
	if (root_i == root_j) return;
	numCCs--;
	if (rnk[i] != rnk[j]) {
		if (rnk[i] > rnk[j]) parent[root_j] = parent[root_i];
		else parent[root_i] = parent[root_j];
	} else {
		parent[root_i] = parent[root_j];
		rnk[j]++;
	}
}

int construct(vvi p) {
	int N = (int) p.size(), numCCs = N;
	vector<int> parent(N), rnk(N, 0);
	for (int i = 0; i < N; i++)
		parent[i] = i;
	for (int i = 0; i < N; i++) {
		for (int j = i+1; j < N; j++) {
			if (p[i][j]) {
				if (p[i][j] == 3) {
					return 0;
				}
				merge(i, j, parent, rnk, numCCs);
			} else if (root(i, parent) == root(j, parent))
				return 0;
		}
	}
	unordered_map<int, int> roots;
	int c = 0;
	for (int i = 0; i < N; i++) {
		if (parent[i] == i)
			roots[i] = c++;
	}

	vvi CCs(numCCs, vi());
	for (int i = 0; i < N; i++)
		CCs[roots[root(i, parent)]].push_back(i);

	int CCsize, u, v, n, root_u, root_v;
	vvi result(N, vi(N, 0)), oneTails;

	for (int i = 0; i < numCCs; i++) {
		CCsize = (int) CCs[i].size();
		if (CCsize == 1) continue;
		n = CCsize;
		for (auto node: CCs[i]) {
			parent[node] = node;
			rnk[node] = 0;
		}
		for (int j = 0; j < CCsize; j++) {
			u = CCs[i][j];
			root_u = root(u, parent);
			for (int k = j+1; k < CCsize; k++) {
				v = CCs[i][k];
				if (p[u][v] == 1)
					merge(u, v, parent, rnk, n);
				else if (root_u == root(v, parent))
					return 0;
			}
		}
		if (n == 2) return 0;

		roots.clear();
		c = 0;
		for (int j = 0; j < CCsize; j++) {
			u = CCs[i][j];
			root_u = root(u, parent);
			if (root_u == u)
				roots[u] = c++;
			for (int k = j+1; k < CCsize; k++) {
				v = CCs[i][k];
				root_v = root(v, parent);
				if (root_u != root_v) {
					if (p[u][v] != 2) {
						return 0;
					}
				} else if (p[u][v] != 1) {
					return 0;
				}
			}
		}

		// connect each tail amongst itself
		oneTails = vvi(n, vi());
		for (int j = 0; j < CCsize; j++)
			oneTails[roots[root(CCs[i][j], parent)]].push_back(CCs[i][j]);
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < (int) oneTails[j].size() - 1; k++)
				result[oneTails[j][k]][oneTails[j][k+1]] = result[oneTails[j][k+1]][oneTails[j][k]] = 1;
		}

		// connect cycle of tail roots
		for (int j = 0; j < n-1; j++)
			result[oneTails[j][0]][oneTails[j+1][0]] = result[oneTails[j+1][0]][oneTails[j][0]] = 1;

		if (n > 1)
			result[oneTails[0][0]][oneTails[n-1][0]] = result[oneTails[n-1][0]][oneTails[0][0]] = 1;
	}
	build(result);
	return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 1260 KB Output is correct
7 Correct 274 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 1260 KB Output is correct
7 Correct 274 ms 22124 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 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 1260 KB Output is correct
13 Correct 300 ms 22252 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 6 ms 620 KB Output is correct
17 Correct 105 ms 8172 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 64 ms 5868 KB Output is correct
21 Correct 252 ms 24172 KB Output is correct
22 Correct 256 ms 24144 KB Output is correct
23 Correct 270 ms 24044 KB Output is correct
24 Correct 271 ms 24172 KB Output is correct
25 Correct 112 ms 10220 KB Output is correct
26 Correct 102 ms 10220 KB Output is correct
27 Correct 264 ms 24044 KB Output is correct
28 Correct 280 ms 24172 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 0 ms 364 KB Output is correct
4 Correct 0 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 12 ms 1260 KB Output is correct
9 Correct 253 ms 22380 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 1280 KB Output is correct
13 Correct 254 ms 22252 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 7 ms 620 KB Output is correct
17 Correct 104 ms 8300 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 0 ms 364 KB Output is correct
21 Correct 64 ms 5868 KB Output is correct
22 Correct 255 ms 22124 KB Output is correct
23 Correct 253 ms 22508 KB Output is correct
24 Correct 266 ms 22124 KB Output is correct
25 Correct 105 ms 12268 KB Output is correct
26 Correct 99 ms 8172 KB Output is correct
27 Correct 247 ms 22124 KB Output is correct
28 Correct 296 ms 22252 KB Output is correct
29 Correct 107 ms 12268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 65 ms 5996 KB Output is correct
5 Correct 266 ms 24172 KB Output is correct
6 Correct 258 ms 24172 KB Output is correct
7 Correct 268 ms 24044 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 67 ms 6380 KB Output is correct
10 Correct 253 ms 24044 KB Output is correct
11 Correct 254 ms 24172 KB Output is correct
12 Correct 263 ms 24172 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 64 ms 6380 KB Output is correct
17 Correct 257 ms 24172 KB Output is correct
18 Correct 254 ms 24044 KB Output is correct
19 Correct 261 ms 24044 KB Output is correct
20 Correct 256 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 1260 KB Output is correct
7 Correct 274 ms 22124 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 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 1260 KB Output is correct
13 Correct 300 ms 22252 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 6 ms 620 KB Output is correct
17 Correct 105 ms 8172 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 64 ms 5868 KB Output is correct
21 Correct 252 ms 24172 KB Output is correct
22 Correct 256 ms 24144 KB Output is correct
23 Correct 270 ms 24044 KB Output is correct
24 Correct 271 ms 24172 KB Output is correct
25 Correct 112 ms 10220 KB Output is correct
26 Correct 102 ms 10220 KB Output is correct
27 Correct 264 ms 24044 KB Output is correct
28 Correct 280 ms 24172 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 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 12 ms 1260 KB Output is correct
37 Correct 253 ms 22380 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1280 KB Output is correct
41 Correct 254 ms 22252 KB Output is correct
42 Correct 0 ms 364 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 7 ms 620 KB Output is correct
45 Correct 104 ms 8300 KB Output is correct
46 Correct 0 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 0 ms 364 KB Output is correct
49 Correct 64 ms 5868 KB Output is correct
50 Correct 255 ms 22124 KB Output is correct
51 Correct 253 ms 22508 KB Output is correct
52 Correct 266 ms 22124 KB Output is correct
53 Correct 105 ms 12268 KB Output is correct
54 Correct 99 ms 8172 KB Output is correct
55 Correct 247 ms 22124 KB Output is correct
56 Correct 296 ms 22252 KB Output is correct
57 Correct 107 ms 12268 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 748 KB Output is correct
61 Correct 122 ms 10220 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 64 ms 6380 KB Output is correct
66 Correct 122 ms 10220 KB Output is correct
67 Correct 100 ms 10220 KB Output is correct
68 Correct 102 ms 10220 KB Output is correct
69 Correct 107 ms 14188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 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 1260 KB Output is correct
7 Correct 274 ms 22124 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 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 1260 KB Output is correct
13 Correct 300 ms 22252 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 6 ms 620 KB Output is correct
17 Correct 105 ms 8172 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 64 ms 5868 KB Output is correct
21 Correct 252 ms 24172 KB Output is correct
22 Correct 256 ms 24144 KB Output is correct
23 Correct 270 ms 24044 KB Output is correct
24 Correct 271 ms 24172 KB Output is correct
25 Correct 112 ms 10220 KB Output is correct
26 Correct 102 ms 10220 KB Output is correct
27 Correct 264 ms 24044 KB Output is correct
28 Correct 280 ms 24172 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 1 ms 364 KB Output is correct
31 Correct 0 ms 364 KB Output is correct
32 Correct 0 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 12 ms 1260 KB Output is correct
37 Correct 253 ms 22380 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 11 ms 1280 KB Output is correct
41 Correct 254 ms 22252 KB Output is correct
42 Correct 0 ms 364 KB Output is correct
43 Correct 0 ms 364 KB Output is correct
44 Correct 7 ms 620 KB Output is correct
45 Correct 104 ms 8300 KB Output is correct
46 Correct 0 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 0 ms 364 KB Output is correct
49 Correct 64 ms 5868 KB Output is correct
50 Correct 255 ms 22124 KB Output is correct
51 Correct 253 ms 22508 KB Output is correct
52 Correct 266 ms 22124 KB Output is correct
53 Correct 105 ms 12268 KB Output is correct
54 Correct 99 ms 8172 KB Output is correct
55 Correct 247 ms 22124 KB Output is correct
56 Correct 296 ms 22252 KB Output is correct
57 Correct 107 ms 12268 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 0 ms 364 KB Output is correct
60 Correct 0 ms 364 KB Output is correct
61 Correct 65 ms 5996 KB Output is correct
62 Correct 266 ms 24172 KB Output is correct
63 Correct 258 ms 24172 KB Output is correct
64 Correct 268 ms 24044 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 67 ms 6380 KB Output is correct
67 Correct 253 ms 24044 KB Output is correct
68 Correct 254 ms 24172 KB Output is correct
69 Correct 263 ms 24172 KB Output is correct
70 Correct 1 ms 492 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 64 ms 6380 KB Output is correct
74 Correct 257 ms 24172 KB Output is correct
75 Correct 254 ms 24044 KB Output is correct
76 Correct 261 ms 24044 KB Output is correct
77 Correct 256 ms 24172 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 748 KB Output is correct
81 Correct 122 ms 10220 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 64 ms 6380 KB Output is correct
86 Correct 122 ms 10220 KB Output is correct
87 Correct 100 ms 10220 KB Output is correct
88 Correct 102 ms 10220 KB Output is correct
89 Correct 107 ms 14188 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 748 KB Output is correct
93 Correct 105 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 30 ms 2796 KB Output is correct
98 Correct 102 ms 10220 KB Output is correct
99 Correct 103 ms 10220 KB Output is correct
100 Correct 100 ms 10220 KB Output is correct
101 Correct 101 ms 10220 KB Output is correct
102 Correct 100 ms 10116 KB Output is correct
103 Correct 100 ms 10220 KB Output is correct
104 Correct 100 ms 10220 KB Output is correct
105 Correct 105 ms 10220 KB Output is correct