Submission #145192

# Submission time Handle Problem Language Result Execution time Memory
145192 2019-08-19T08:39:45 Z ecnerwala Split the Attractions (IOI19_split) C++14
100 / 100
188 ms 16552 KB
#include "split.h"

#include <bits/stdc++.h>
using namespace std;

struct union_find {
	vector<int> par;
	vector<int> sz;
	explicit union_find(int N) : par(N, -1), sz(N, 1) { }

	int get_par(int a) {
		return (par[a] == -1) ? a : (par[a] = get_par(par[a]));
	}

	pair<bool, int> merge(int a, int b) {
		a = get_par(a), b = get_par(b);
		if (a == b) return {false, sz[a]};
		if (sz[a] < sz[b]) swap(a, b);
		par[b] = a;
		sz[a] += sz[b];
		return {true, sz[a]};
	}
};

vector<int> my_find_split(int N, int Asz, int Bsz, int Csz, vector<int> U, vector<int> V) {
	int M = int(U.size());
	vector<pair<int, int>> sizes({{Asz, 1}, {Bsz, 2}, {Csz, 3}});
	sort(sizes.begin(), sizes.end());
	Asz = sizes[0].first, Bsz = sizes[1].first, Csz = sizes[2].first;
	int Alabel = sizes[0].second, Blabel = sizes[1].second, Clabel = sizes[2].second;

	vector<vector<int>> adj(N);
	for (int i = 0; i < M; i++) {
		adj[U[i]].push_back(V[i]);
		adj[V[i]].push_back(U[i]);
	}

	vector<int> par(N, -2);
	vector<int> q; q.reserve(N);
	par[0] = -1;
	q.push_back(0);
	for (int i = 0; i < N; i++) {
		int cur = q[i];
		for (int nxt : adj[cur]) {
			if (par[nxt] != -2) continue;
			par[nxt] = cur;
			q.push_back(nxt);
		}
	}

	vector<int> sz(N);
	for (int i = N-1; i >= 0; i--) {
		int cur = q[i];
		sz[cur]++;
		if (par[cur] != -1) sz[par[cur]] += sz[cur];
	}

	int centroid = -1;
	for (int i = N-1; i >= 0; i--) {
		int cur = q[i];
		if (sz[cur] * 2 >= N) {
			centroid = cur;
			break;
		}
	}
	assert(centroid != -1);

	union_find uf(N);
	for (int i = N-1; i >= 1; i--) {
		int cur = q[i];
		assert(par[cur] != -1);
		if (cur == centroid || par[cur] == centroid) continue;
		uf.merge(cur, par[cur]);
	}

	int Astart = -1;

	for (int i = 0; i < N; i++) {
		if (i == centroid) continue;
		if (uf.sz[uf.get_par(i)] >= Asz) {
			// this is it
			Astart = i;
			goto found_a;
		}
	}
	for (int e = 0; e < M; e++) {
		if (U[e] == centroid || V[e] == centroid) continue;
		if (uf.merge(U[e], V[e]).second >= Asz) {
			Astart = U[e];
			goto found_a;
		}
	}

	return vector<int>(N, 0);

found_a:
	vector<int> res(N, Clabel);
	{
		vector<bool> vis(N, false);
		q = vector<int>({Astart});
		vis[Astart] = true;
		for (int i = 0; i < Asz; i++) {
			int cur = q[i];
			res[cur] = Alabel;
			for (int nxt : adj[cur]) {
				if (uf.get_par(nxt) != uf.get_par(Astart)) continue;
				if (vis[nxt]) continue;
				vis[nxt] = true;
				q.push_back(nxt);
			}
		}
	}
	{
		vector<bool> vis(N, false);
		q = vector<int>({centroid});
		vis[centroid] = true;
		for (int i = 0; i < Bsz; i++) {
			int cur = q[i];
			res[cur] = Blabel;
			for (int nxt : adj[cur]) {
				if (uf.get_par(nxt) == uf.get_par(Astart)) continue;
				if (vis[nxt]) continue;
				vis[nxt] = true;
				q.push_back(nxt);
			}
		}
	}
	return res;
}

vector<int> find_split(int N, int Asz, int Bsz, int Csz, vector<int> U, vector<int> V) {
	vector<int> res = my_find_split(N, Asz, Bsz, Csz, U, V);
	if (res != vector<int>(N, 0)) {
		int cnt[4] = {0,0,0,0};
		for (int i = 0; i < N; i++) {
			assert(1 <= res[i] && res[i] <= 3);
			cnt[res[i]] ++;
		}
		assert(cnt[1] == Asz);
		assert(cnt[2] == Bsz);
		assert(cnt[3] == Csz);

		union_find uf(N);
		for (int e = 0; e < int(U.size()); e++) {
			if (res[U[e]] != res[V[e]]) continue;
			uf.merge(U[e], V[e]);
		}
		int numGood = 0;
		for (int z = 1; z <= 3; z++) {
			int c = 0;
			for (int i = 0; i < N; i++) {
				if (res[i] == z) {
					c += uf.get_par(i) == i;
				}
			}
			assert(c >= 1);
			if (c == 1) {
				numGood ++;
			}
		}
		assert(numGood >= 2);
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB ok, correct split
2 Correct 2 ms 376 KB ok, correct split
3 Correct 2 ms 376 KB ok, correct split
4 Correct 2 ms 376 KB ok, correct split
5 Correct 2 ms 256 KB ok, correct split
6 Correct 2 ms 256 KB ok, correct split
7 Correct 112 ms 10504 KB ok, correct split
8 Correct 106 ms 10488 KB ok, correct split
9 Correct 106 ms 10572 KB ok, correct split
10 Correct 114 ms 10508 KB ok, correct split
11 Correct 118 ms 10576 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 2 ms 256 KB ok, correct split
3 Correct 2 ms 256 KB ok, correct split
4 Correct 120 ms 12088 KB ok, correct split
5 Correct 106 ms 10736 KB ok, correct split
6 Correct 99 ms 10488 KB ok, correct split
7 Correct 106 ms 10612 KB ok, correct split
8 Correct 167 ms 13908 KB ok, correct split
9 Correct 124 ms 10572 KB ok, correct split
10 Correct 75 ms 11496 KB ok, correct split
11 Correct 76 ms 11368 KB ok, correct split
12 Correct 76 ms 11376 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 112 ms 10752 KB ok, correct split
3 Correct 44 ms 4600 KB ok, correct split
4 Correct 2 ms 376 KB ok, correct split
5 Correct 120 ms 10540 KB ok, correct split
6 Correct 111 ms 10468 KB ok, correct split
7 Correct 106 ms 10608 KB ok, correct split
8 Correct 127 ms 10512 KB ok, correct split
9 Correct 111 ms 10608 KB ok, correct split
10 Correct 28 ms 3704 KB ok, no valid answer
11 Correct 42 ms 5512 KB ok, no valid answer
12 Correct 83 ms 10872 KB ok, no valid answer
13 Correct 102 ms 10724 KB ok, no valid answer
14 Correct 70 ms 10928 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB ok, correct split
2 Correct 2 ms 376 KB ok, no valid answer
3 Correct 2 ms 256 KB ok, correct split
4 Correct 3 ms 376 KB ok, correct split
5 Correct 2 ms 256 KB ok, correct split
6 Correct 2 ms 296 KB ok, correct split
7 Correct 2 ms 256 KB ok, correct split
8 Correct 2 ms 256 KB ok, correct split
9 Correct 4 ms 632 KB ok, correct split
10 Correct 5 ms 632 KB ok, correct split
11 Correct 2 ms 376 KB ok, correct split
12 Correct 4 ms 632 KB ok, correct split
13 Correct 2 ms 256 KB ok, correct split
14 Correct 2 ms 256 KB ok, correct split
15 Correct 2 ms 256 KB ok, correct split
16 Correct 2 ms 256 KB ok, correct split
17 Correct 2 ms 252 KB ok, correct split
18 Correct 2 ms 256 KB ok, correct split
19 Correct 2 ms 376 KB ok, correct split
20 Correct 3 ms 376 KB ok, correct split
21 Correct 4 ms 632 KB ok, correct split
22 Correct 4 ms 632 KB ok, correct split
23 Correct 4 ms 504 KB ok, correct split
24 Correct 4 ms 504 KB ok, correct split
25 Correct 4 ms 504 KB ok, correct split
26 Correct 4 ms 632 KB ok, correct split
27 Correct 4 ms 636 KB ok, correct split
28 Correct 4 ms 632 KB ok, correct split
29 Correct 9 ms 376 KB ok, correct split
30 Correct 5 ms 760 KB ok, correct split
31 Correct 3 ms 504 KB ok, correct split
32 Correct 2 ms 376 KB ok, correct split
33 Correct 2 ms 376 KB ok, correct split
34 Correct 4 ms 632 KB ok, correct split
35 Correct 4 ms 632 KB ok, correct split
36 Correct 4 ms 632 KB ok, correct split
37 Correct 5 ms 760 KB ok, correct split
38 Correct 5 ms 632 KB ok, correct split
39 Correct 5 ms 696 KB ok, correct split
40 Correct 5 ms 632 KB ok, correct split
41 Correct 3 ms 504 KB ok, correct split
42 Correct 3 ms 504 KB ok, correct split
43 Correct 5 ms 760 KB ok, correct split
44 Correct 5 ms 888 KB ok, correct split
45 Correct 5 ms 632 KB ok, correct split
46 Correct 4 ms 632 KB ok, correct split
47 Correct 4 ms 632 KB ok, no valid answer
48 Correct 4 ms 632 KB ok, correct split
49 Correct 4 ms 632 KB ok, correct split
50 Correct 2 ms 348 KB ok, no valid answer
51 Correct 2 ms 376 KB ok, no valid answer
52 Correct 4 ms 632 KB ok, no valid answer
53 Correct 5 ms 632 KB ok, no valid answer
54 Correct 4 ms 632 KB ok, no valid answer
55 Correct 4 ms 632 KB ok, no valid answer
56 Correct 4 ms 632 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB ok, correct split
2 Correct 2 ms 376 KB ok, correct split
3 Correct 2 ms 376 KB ok, correct split
4 Correct 2 ms 376 KB ok, correct split
5 Correct 2 ms 256 KB ok, correct split
6 Correct 2 ms 256 KB ok, correct split
7 Correct 112 ms 10504 KB ok, correct split
8 Correct 106 ms 10488 KB ok, correct split
9 Correct 106 ms 10572 KB ok, correct split
10 Correct 114 ms 10508 KB ok, correct split
11 Correct 118 ms 10576 KB ok, correct split
12 Correct 2 ms 256 KB ok, correct split
13 Correct 2 ms 256 KB ok, correct split
14 Correct 2 ms 256 KB ok, correct split
15 Correct 120 ms 12088 KB ok, correct split
16 Correct 106 ms 10736 KB ok, correct split
17 Correct 99 ms 10488 KB ok, correct split
18 Correct 106 ms 10612 KB ok, correct split
19 Correct 167 ms 13908 KB ok, correct split
20 Correct 124 ms 10572 KB ok, correct split
21 Correct 75 ms 11496 KB ok, correct split
22 Correct 76 ms 11368 KB ok, correct split
23 Correct 76 ms 11376 KB ok, correct split
24 Correct 2 ms 256 KB ok, correct split
25 Correct 112 ms 10752 KB ok, correct split
26 Correct 44 ms 4600 KB ok, correct split
27 Correct 2 ms 376 KB ok, correct split
28 Correct 120 ms 10540 KB ok, correct split
29 Correct 111 ms 10468 KB ok, correct split
30 Correct 106 ms 10608 KB ok, correct split
31 Correct 127 ms 10512 KB ok, correct split
32 Correct 111 ms 10608 KB ok, correct split
33 Correct 28 ms 3704 KB ok, no valid answer
34 Correct 42 ms 5512 KB ok, no valid answer
35 Correct 83 ms 10872 KB ok, no valid answer
36 Correct 102 ms 10724 KB ok, no valid answer
37 Correct 70 ms 10928 KB ok, no valid answer
38 Correct 2 ms 376 KB ok, correct split
39 Correct 2 ms 376 KB ok, no valid answer
40 Correct 2 ms 256 KB ok, correct split
41 Correct 3 ms 376 KB ok, correct split
42 Correct 2 ms 256 KB ok, correct split
43 Correct 2 ms 296 KB ok, correct split
44 Correct 2 ms 256 KB ok, correct split
45 Correct 2 ms 256 KB ok, correct split
46 Correct 4 ms 632 KB ok, correct split
47 Correct 5 ms 632 KB ok, correct split
48 Correct 2 ms 376 KB ok, correct split
49 Correct 4 ms 632 KB ok, correct split
50 Correct 2 ms 256 KB ok, correct split
51 Correct 2 ms 256 KB ok, correct split
52 Correct 2 ms 256 KB ok, correct split
53 Correct 2 ms 256 KB ok, correct split
54 Correct 2 ms 252 KB ok, correct split
55 Correct 2 ms 256 KB ok, correct split
56 Correct 2 ms 376 KB ok, correct split
57 Correct 3 ms 376 KB ok, correct split
58 Correct 4 ms 632 KB ok, correct split
59 Correct 4 ms 632 KB ok, correct split
60 Correct 4 ms 504 KB ok, correct split
61 Correct 4 ms 504 KB ok, correct split
62 Correct 4 ms 504 KB ok, correct split
63 Correct 4 ms 632 KB ok, correct split
64 Correct 4 ms 636 KB ok, correct split
65 Correct 4 ms 632 KB ok, correct split
66 Correct 9 ms 376 KB ok, correct split
67 Correct 5 ms 760 KB ok, correct split
68 Correct 3 ms 504 KB ok, correct split
69 Correct 2 ms 376 KB ok, correct split
70 Correct 2 ms 376 KB ok, correct split
71 Correct 4 ms 632 KB ok, correct split
72 Correct 4 ms 632 KB ok, correct split
73 Correct 4 ms 632 KB ok, correct split
74 Correct 5 ms 760 KB ok, correct split
75 Correct 5 ms 632 KB ok, correct split
76 Correct 5 ms 696 KB ok, correct split
77 Correct 5 ms 632 KB ok, correct split
78 Correct 3 ms 504 KB ok, correct split
79 Correct 3 ms 504 KB ok, correct split
80 Correct 5 ms 760 KB ok, correct split
81 Correct 5 ms 888 KB ok, correct split
82 Correct 5 ms 632 KB ok, correct split
83 Correct 4 ms 632 KB ok, correct split
84 Correct 4 ms 632 KB ok, no valid answer
85 Correct 4 ms 632 KB ok, correct split
86 Correct 4 ms 632 KB ok, correct split
87 Correct 2 ms 348 KB ok, no valid answer
88 Correct 2 ms 376 KB ok, no valid answer
89 Correct 4 ms 632 KB ok, no valid answer
90 Correct 5 ms 632 KB ok, no valid answer
91 Correct 4 ms 632 KB ok, no valid answer
92 Correct 4 ms 632 KB ok, no valid answer
93 Correct 4 ms 632 KB ok, no valid answer
94 Correct 135 ms 12604 KB ok, correct split
95 Correct 168 ms 16368 KB ok, correct split
96 Correct 159 ms 14960 KB ok, correct split
97 Correct 44 ms 5080 KB ok, correct split
98 Correct 40 ms 5112 KB ok, correct split
99 Correct 63 ms 7132 KB ok, correct split
100 Correct 165 ms 16376 KB ok, correct split
101 Correct 153 ms 14852 KB ok, correct split
102 Correct 125 ms 14956 KB ok, correct split
103 Correct 116 ms 14708 KB ok, correct split
104 Correct 148 ms 16552 KB ok, correct split
105 Correct 65 ms 7468 KB ok, correct split
106 Correct 126 ms 16256 KB ok, correct split
107 Correct 117 ms 11892 KB ok, correct split
108 Correct 118 ms 11824 KB ok, correct split
109 Correct 174 ms 15860 KB ok, correct split
110 Correct 152 ms 15948 KB ok, correct split
111 Correct 167 ms 16008 KB ok, correct split
112 Correct 163 ms 16216 KB ok, correct split
113 Correct 160 ms 16328 KB ok, correct split
114 Correct 15 ms 2140 KB ok, correct split
115 Correct 15 ms 1844 KB ok, correct split
116 Correct 140 ms 16108 KB ok, correct split
117 Correct 145 ms 15956 KB ok, correct split
118 Correct 128 ms 11640 KB ok, correct split
119 Correct 110 ms 11708 KB ok, correct split
120 Correct 127 ms 11760 KB ok, correct split
121 Correct 110 ms 11808 KB ok, no valid answer
122 Correct 90 ms 11800 KB ok, no valid answer
123 Correct 188 ms 16244 KB ok, no valid answer
124 Correct 154 ms 16200 KB ok, no valid answer
125 Correct 92 ms 13140 KB ok, no valid answer
126 Correct 64 ms 10960 KB ok, no valid answer
127 Correct 119 ms 13860 KB ok, no valid answer