Submission #298669

# Submission time Handle Problem Language Result Execution time Memory
298669 2020-09-13T18:26:18 Z keko37 Split the Attractions (IOI19_split) C++14
100 / 100
250 ms 19428 KB
#include<bits/stdc++.h>
#include "split.h"

using namespace std;

typedef vector <int> vi;
typedef pair <int, int> pi;

const int MAXN = 200005;

int n, m, root, cnt;
pi col[3];
int par[MAXN], siz[MAXN], sol[MAXN];
vector <int> v[MAXN];
vector <pi> e;
set <pi> st;

int nadi (int x) {
	if (x == par[x]) return x;
	return par[x] = nadi(par[x]);
}

bool spoji (int a, int b) {
	a = nadi(a); b = nadi(b);
	if (a == b) return 0;
	par[a] = b;
	siz[b] += siz[a];
	return 1;
}

void dfs (int x, int rod) {
	siz[x] = 1;
	for (auto sus : v[x]) {
		if (sus == rod) continue;
		dfs(sus, x);
		siz[x] += siz[sus];
	}
}

int nadi_centroid (int x, int rod) {
	int mx = 0, ind = 0;
	for (auto sus : v[x]) {
		if (sus == rod) continue;
		if (siz[sus] > mx) {
			mx = siz[sus];
			ind = sus;
		}
	}
	if (mx * 2 <= n) return x;
	return nadi_centroid(ind, x);
}

bool check () {
	if (st.empty()) return 0;
	auto it = st.end(); it--;
	return (it -> first) >= col[0].first;
}

void oboji (int x) {
	if (cnt == col[1].first) return;
	sol[x] = col[1].second;
	cnt++;
	for (auto sus : v[x]) {
		if (cnt == col[1].first) return;
		if (sol[sus] == 0) oboji(sus);
	}
}

int p;

void small (int x) {
	if (cnt == col[0].first) return;
	sol[x] = col[0].second;
	cnt++;
	for (auto sus : v[x]) {
		if (cnt == col[0].first) return;
		if (sol[sus] == 0 && nadi(sus) == p) small(sus);
	}
}

vi solve () {
	auto it = st.end(); it--;
	p = it -> second;
	cnt = 0; small(p);
	cnt = 0; oboji(root);
	vi res(n);
	for (int i = 1; i <= n; i++) {
		if (sol[i] == 0) sol[i] = col[2].second;
		res[i - 1] = sol[i];
	}
	return res;
}

vi find_split (int N, int A, int B, int C, vi P, vi Q) {
	n = N; m = P.size();
	col[0] = {A, 1}; col[1] = {B, 2}; col[2] = {C, 3};
	sort(col, col + 3);
	for (int i = 1; i <= n; i++) {
		par[i] = i;
	}
	for (int i = 0; i < m; i++) {
		P[i]++; Q[i]++;
		if (spoji(P[i], Q[i])) {
			v[P[i]].push_back(Q[i]);
			v[Q[i]].push_back(P[i]);
		} else {
			e.push_back({P[i], Q[i]});
		}
	}
	dfs(1, 0);
	root = nadi_centroid(1, 0);
	for (int i = 1; i <= n; i++) {
		par[i] = i;
		siz[i] = 1;
	}
	for (int i = 1; i <= n; i++) {
		for (auto sus : v[i]) {
			if (i != root && sus != root && i < sus) spoji(i, sus);
		}
	}
	for (auto sus : v[root]) {
		st.insert({siz[nadi(sus)], nadi(sus)});
	}
	if (check()) return solve();
	for (auto edge : e) {
		int a = edge.first, b = edge.second;
		int pa = nadi(a), pb = nadi(b);
		if (a == root || b == root || pa == pb) continue;
		st.erase({siz[pa], pa});
		st.erase({siz[pb], pb});
		par[pa] = pb;
		siz[pb] += siz[pa];
		st.insert({siz[pb], pb});
		v[a].push_back(b);
		v[b].push_back(a);
		if (check()) return solve();
	}
	vi res(n);
	return res;
}




# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB ok, correct split
2 Correct 4 ms 4992 KB ok, correct split
3 Correct 3 ms 4992 KB ok, correct split
4 Correct 4 ms 4992 KB ok, correct split
5 Correct 4 ms 4992 KB ok, correct split
6 Correct 4 ms 4992 KB ok, correct split
7 Correct 123 ms 17784 KB ok, correct split
8 Correct 110 ms 15864 KB ok, correct split
9 Correct 116 ms 15480 KB ok, correct split
10 Correct 111 ms 17808 KB ok, correct split
11 Correct 118 ms 16892 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB ok, correct split
2 Correct 4 ms 4992 KB ok, correct split
3 Correct 4 ms 4992 KB ok, correct split
4 Correct 122 ms 13040 KB ok, correct split
5 Correct 104 ms 11768 KB ok, correct split
6 Correct 111 ms 17660 KB ok, correct split
7 Correct 117 ms 15608 KB ok, correct split
8 Correct 146 ms 14188 KB ok, correct split
9 Correct 109 ms 11768 KB ok, correct split
10 Correct 116 ms 16900 KB ok, correct split
11 Correct 112 ms 16756 KB ok, correct split
12 Correct 117 ms 16824 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB ok, correct split
2 Correct 103 ms 11768 KB ok, correct split
3 Correct 36 ms 8056 KB ok, correct split
4 Correct 4 ms 4992 KB ok, correct split
5 Correct 119 ms 13816 KB ok, correct split
6 Correct 111 ms 13680 KB ok, correct split
7 Correct 111 ms 13584 KB ok, correct split
8 Correct 112 ms 14456 KB ok, correct split
9 Correct 109 ms 13304 KB ok, correct split
10 Correct 30 ms 7544 KB ok, no valid answer
11 Correct 43 ms 8696 KB ok, no valid answer
12 Correct 102 ms 14420 KB ok, no valid answer
13 Correct 97 ms 11896 KB ok, no valid answer
14 Correct 112 ms 16368 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB ok, correct split
2 Correct 3 ms 4992 KB ok, no valid answer
3 Correct 4 ms 4992 KB ok, correct split
4 Correct 4 ms 4992 KB ok, correct split
5 Correct 4 ms 4992 KB ok, correct split
6 Correct 4 ms 4992 KB ok, correct split
7 Correct 4 ms 4992 KB ok, correct split
8 Correct 4 ms 4992 KB ok, correct split
9 Correct 6 ms 5248 KB ok, correct split
10 Correct 6 ms 5248 KB ok, correct split
11 Correct 4 ms 4992 KB ok, correct split
12 Correct 7 ms 5376 KB ok, correct split
13 Correct 4 ms 4992 KB ok, correct split
14 Correct 4 ms 4992 KB ok, correct split
15 Correct 4 ms 5120 KB ok, correct split
16 Correct 4 ms 4992 KB ok, correct split
17 Correct 4 ms 4992 KB ok, correct split
18 Correct 4 ms 4992 KB ok, correct split
19 Correct 4 ms 5248 KB ok, correct split
20 Correct 4 ms 5120 KB ok, correct split
21 Correct 5 ms 5248 KB ok, correct split
22 Correct 5 ms 5248 KB ok, correct split
23 Correct 5 ms 5248 KB ok, correct split
24 Correct 5 ms 5280 KB ok, correct split
25 Correct 5 ms 5248 KB ok, correct split
26 Correct 6 ms 5504 KB ok, correct split
27 Correct 6 ms 5376 KB ok, correct split
28 Correct 6 ms 5376 KB ok, correct split
29 Correct 4 ms 5120 KB ok, correct split
30 Correct 6 ms 5376 KB ok, correct split
31 Correct 4 ms 5120 KB ok, correct split
32 Correct 4 ms 4992 KB ok, correct split
33 Correct 4 ms 5120 KB ok, correct split
34 Correct 6 ms 5248 KB ok, correct split
35 Correct 6 ms 5248 KB ok, correct split
36 Correct 5 ms 5248 KB ok, correct split
37 Correct 7 ms 5376 KB ok, correct split
38 Correct 7 ms 5376 KB ok, correct split
39 Correct 7 ms 5376 KB ok, correct split
40 Correct 7 ms 5376 KB ok, correct split
41 Correct 6 ms 5120 KB ok, correct split
42 Correct 5 ms 5120 KB ok, correct split
43 Correct 6 ms 5376 KB ok, correct split
44 Correct 7 ms 5376 KB ok, correct split
45 Correct 6 ms 5504 KB ok, correct split
46 Correct 5 ms 5248 KB ok, correct split
47 Correct 5 ms 5248 KB ok, no valid answer
48 Correct 5 ms 5248 KB ok, correct split
49 Correct 5 ms 5248 KB ok, correct split
50 Correct 4 ms 4992 KB ok, no valid answer
51 Correct 4 ms 5120 KB ok, no valid answer
52 Correct 5 ms 5248 KB ok, no valid answer
53 Correct 6 ms 5248 KB ok, no valid answer
54 Correct 5 ms 5376 KB ok, no valid answer
55 Correct 6 ms 5376 KB ok, no valid answer
56 Correct 6 ms 5376 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB ok, correct split
2 Correct 4 ms 4992 KB ok, correct split
3 Correct 3 ms 4992 KB ok, correct split
4 Correct 4 ms 4992 KB ok, correct split
5 Correct 4 ms 4992 KB ok, correct split
6 Correct 4 ms 4992 KB ok, correct split
7 Correct 123 ms 17784 KB ok, correct split
8 Correct 110 ms 15864 KB ok, correct split
9 Correct 116 ms 15480 KB ok, correct split
10 Correct 111 ms 17808 KB ok, correct split
11 Correct 118 ms 16892 KB ok, correct split
12 Correct 4 ms 5120 KB ok, correct split
13 Correct 4 ms 4992 KB ok, correct split
14 Correct 4 ms 4992 KB ok, correct split
15 Correct 122 ms 13040 KB ok, correct split
16 Correct 104 ms 11768 KB ok, correct split
17 Correct 111 ms 17660 KB ok, correct split
18 Correct 117 ms 15608 KB ok, correct split
19 Correct 146 ms 14188 KB ok, correct split
20 Correct 109 ms 11768 KB ok, correct split
21 Correct 116 ms 16900 KB ok, correct split
22 Correct 112 ms 16756 KB ok, correct split
23 Correct 117 ms 16824 KB ok, correct split
24 Correct 4 ms 4992 KB ok, correct split
25 Correct 103 ms 11768 KB ok, correct split
26 Correct 36 ms 8056 KB ok, correct split
27 Correct 4 ms 4992 KB ok, correct split
28 Correct 119 ms 13816 KB ok, correct split
29 Correct 111 ms 13680 KB ok, correct split
30 Correct 111 ms 13584 KB ok, correct split
31 Correct 112 ms 14456 KB ok, correct split
32 Correct 109 ms 13304 KB ok, correct split
33 Correct 30 ms 7544 KB ok, no valid answer
34 Correct 43 ms 8696 KB ok, no valid answer
35 Correct 102 ms 14420 KB ok, no valid answer
36 Correct 97 ms 11896 KB ok, no valid answer
37 Correct 112 ms 16368 KB ok, no valid answer
38 Correct 4 ms 4992 KB ok, correct split
39 Correct 3 ms 4992 KB ok, no valid answer
40 Correct 4 ms 4992 KB ok, correct split
41 Correct 4 ms 4992 KB ok, correct split
42 Correct 4 ms 4992 KB ok, correct split
43 Correct 4 ms 4992 KB ok, correct split
44 Correct 4 ms 4992 KB ok, correct split
45 Correct 4 ms 4992 KB ok, correct split
46 Correct 6 ms 5248 KB ok, correct split
47 Correct 6 ms 5248 KB ok, correct split
48 Correct 4 ms 4992 KB ok, correct split
49 Correct 7 ms 5376 KB ok, correct split
50 Correct 4 ms 4992 KB ok, correct split
51 Correct 4 ms 4992 KB ok, correct split
52 Correct 4 ms 5120 KB ok, correct split
53 Correct 4 ms 4992 KB ok, correct split
54 Correct 4 ms 4992 KB ok, correct split
55 Correct 4 ms 4992 KB ok, correct split
56 Correct 4 ms 5248 KB ok, correct split
57 Correct 4 ms 5120 KB ok, correct split
58 Correct 5 ms 5248 KB ok, correct split
59 Correct 5 ms 5248 KB ok, correct split
60 Correct 5 ms 5248 KB ok, correct split
61 Correct 5 ms 5280 KB ok, correct split
62 Correct 5 ms 5248 KB ok, correct split
63 Correct 6 ms 5504 KB ok, correct split
64 Correct 6 ms 5376 KB ok, correct split
65 Correct 6 ms 5376 KB ok, correct split
66 Correct 4 ms 5120 KB ok, correct split
67 Correct 6 ms 5376 KB ok, correct split
68 Correct 4 ms 5120 KB ok, correct split
69 Correct 4 ms 4992 KB ok, correct split
70 Correct 4 ms 5120 KB ok, correct split
71 Correct 6 ms 5248 KB ok, correct split
72 Correct 6 ms 5248 KB ok, correct split
73 Correct 5 ms 5248 KB ok, correct split
74 Correct 7 ms 5376 KB ok, correct split
75 Correct 7 ms 5376 KB ok, correct split
76 Correct 7 ms 5376 KB ok, correct split
77 Correct 7 ms 5376 KB ok, correct split
78 Correct 6 ms 5120 KB ok, correct split
79 Correct 5 ms 5120 KB ok, correct split
80 Correct 6 ms 5376 KB ok, correct split
81 Correct 7 ms 5376 KB ok, correct split
82 Correct 6 ms 5504 KB ok, correct split
83 Correct 5 ms 5248 KB ok, correct split
84 Correct 5 ms 5248 KB ok, no valid answer
85 Correct 5 ms 5248 KB ok, correct split
86 Correct 5 ms 5248 KB ok, correct split
87 Correct 4 ms 4992 KB ok, no valid answer
88 Correct 4 ms 5120 KB ok, no valid answer
89 Correct 5 ms 5248 KB ok, no valid answer
90 Correct 6 ms 5248 KB ok, no valid answer
91 Correct 5 ms 5376 KB ok, no valid answer
92 Correct 6 ms 5376 KB ok, no valid answer
93 Correct 6 ms 5376 KB ok, no valid answer
94 Correct 115 ms 13300 KB ok, correct split
95 Correct 151 ms 15984 KB ok, correct split
96 Correct 147 ms 15212 KB ok, correct split
97 Correct 36 ms 8056 KB ok, correct split
98 Correct 38 ms 8184 KB ok, correct split
99 Correct 57 ms 9968 KB ok, correct split
100 Correct 148 ms 16236 KB ok, correct split
101 Correct 139 ms 15212 KB ok, correct split
102 Correct 153 ms 17812 KB ok, correct split
103 Correct 153 ms 17776 KB ok, correct split
104 Correct 127 ms 17392 KB ok, correct split
105 Correct 60 ms 9848 KB ok, correct split
106 Correct 135 ms 19428 KB ok, correct split
107 Correct 107 ms 12664 KB ok, correct split
108 Correct 115 ms 12664 KB ok, correct split
109 Correct 150 ms 15724 KB ok, correct split
110 Correct 223 ms 18412 KB ok, correct split
111 Correct 250 ms 18412 KB ok, correct split
112 Correct 228 ms 18540 KB ok, correct split
113 Correct 233 ms 18540 KB ok, correct split
114 Correct 19 ms 6400 KB ok, correct split
115 Correct 18 ms 6432 KB ok, correct split
116 Correct 153 ms 18284 KB ok, correct split
117 Correct 161 ms 18092 KB ok, correct split
118 Correct 108 ms 12536 KB ok, correct split
119 Correct 99 ms 12628 KB ok, correct split
120 Correct 113 ms 14968 KB ok, correct split
121 Correct 96 ms 12324 KB ok, no valid answer
122 Correct 100 ms 13176 KB ok, no valid answer
123 Correct 148 ms 15724 KB ok, no valid answer
124 Correct 142 ms 15852 KB ok, no valid answer
125 Correct 153 ms 17264 KB ok, no valid answer
126 Correct 97 ms 16112 KB ok, no valid answer
127 Correct 156 ms 17260 KB ok, no valid answer