Submission #143521

# Submission time Handle Problem Language Result Execution time Memory
143521 2019-08-14T13:13:57 Z mosesmayer Split the Attractions (IOI19_split) C++17
100 / 100
212 ms 20176 KB
#include "split.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define ALL(x) x.begin(), x.end()
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;

struct Dsu{
	int n;
	vi par;
	Dsu() {}
	Dsu(int n): n(n), par(n) {}
	void init(){
		iota(par.begin(), par.end(), 0);
	}
	int fpr(int u){
		return par[u] == u ? u : par[u] = fpr(par[u]);
	}
	bool merge(int u, int v){
		if ((u = fpr(u)) == (v = fpr(v))) return 0;
		par[v] = u;
		return 1;
	}
};

const int mxsz = 1e5 + 5;
int n, m;
Dsu dsu;
vector<pii> ord, edges;
vi g[mxsz], adj[mxsz];
vi ans;
int sz[mxsz];
int bfspar[mxsz], bfsord[mxsz];

void buildSpanningTree(int st){
	fill(bfspar, bfspar + n, -1);
	queue<int> q;
	q.push(st);
	bfspar[st] = st;
	int cntr = 0;
	while (!q.empty()){
		int u = q.front(); q.pop();
		bfsord[cntr++] = u; sz[u] = 1;
		for (int nx : g[u]){
			if (bfspar[nx] == -1){
				bfspar[nx] = u;
				adj[u].pb(nx);
				adj[nx].pb(u);
				q.push(nx);
			}
		}
	}
	bfspar[st] = -1;
	for (int i = n-1; i > 0; --i)
		sz[bfspar[bfsord[i]]] += sz[bfsord[i]];
}
int findCentroid(){
	int cpar = -1, u = 0;
	while (cpar != u){
		bool work = 0;
		for (int nx : adj[u]){
			if (nx == cpar) continue;
			if (sz[nx] > n / 2){
				cpar = u; u = nx; work = 1; break;
			}
		}
		if (!work) cpar = u;
	}
	return u;
}
void get_sz(int u, int par){
	sz[u] = 1;
	for (int nx : adj[u]){
		if (nx == par) continue;
		get_sz(nx, u);
		sz[u] += sz[nx];
	}
}
vi res;
queue<int> q;
void workA(int st, int prv){
//	puts("Run1");
	q.push(st);
	for (int i = 0; i < ord[0].fi; i++){
		int u = q.front(); q.pop();
		if (res[u] != 0) {
			i--; continue;
		}
//		printf("%d ", u);
		res[u] = ord[0].se;
		for (int nx : adj[u]){
			if (nx != prv && res[nx] == 0){
				q.push(nx);
			}
		}
	}
	while (!q.empty()) q.pop();
//	puts("\nRun2");
	q.push(prv);
	for (int i = 0; i < ord[1].fi; i++){
		int u = q.front(); q.pop();
		if (res[u] != 0) {
			i--; continue;
		}
//		printf("%d ", u);
		res[u] = ord[1].se;
		for (int nx : adj[u]){
			if (nx != st && res[nx] == 0){
				q.push(nx);
			}
		}
	}
//	puts("\nDone");
	for (int i = 0; i < n; i++)
		if (res[i] == 0) res[i] = ord[2].se;
}
void work(int x, int cen){
//	printf("Work %d cen %d\n", x, cen);
	q.push(x);
//	puts("W1");
	for (int i = 0; i < ord[0].fi; i++){
		int u = q.front(); q.pop();
		if (res[u] != 0) {
			i--; continue;
		}
//		printf("%d ", u);
		res[u] = ord[0].se;
		for (int nx : g[u]){
			if (res[nx] == 0 && dsu.fpr(nx) == x){
				q.push(nx);
			}
		}
	}
	while (!q.empty()) q.pop();
//	puts("\nW2");
	q.push(cen);
	for (int i = 0; i < ord[1].fi; i++){
		int u = q.front(); q.pop();
		if (res[u] != 0) {
			i--; continue;
		}
//		printf("%d ", u);
		res[u] = ord[1].se;
		for (int nx : g[u]){
			if (res[nx] == 0){
				q.push(nx);
			}
		}
	}
//	puts("\nDone");
	for (int i = 0; i < n; i++)
		if (res[i] == 0) res[i] = ord[2].se;
}

void compressRegion(int u, int head, int prv){
	dsu.merge(head, u);
//	printf("%d %d %d\n", u, head, prv);
	for (int nx : adj[u]){
		if (nx != prv) compressRegion(nx, head, u);
	}
}

void Main(){
//	for (int i = 0; i < n; i++){
//		printf("%d: ", i);
//		for (int nx : g[i]){
//			printf(" %d", nx);
//		}
//		puts("");
//	}
	buildSpanningTree(0);
//	puts("Spanning tree:");
//	for (int i = 0; i < n; i++){
//		printf("%d: ", i);
//		for (int nx : adj[i]){
//			printf(" %d", nx);
//		}
//		puts("");
//	}
	int cen = findCentroid();
//	printf("Centroid: %d\n", cen);
	get_sz(cen, -1);
	assert(sz[cen] == n);
	res.resize(n); fill(ALL(res), 0);
	for (int nx : adj[cen]){
		if (sz[nx] >= ord[0].fi){ // has subtree >= a
//			printf("Case 1: %d\n", nx);
			workA(nx, cen);
			return;
		}
	}
	for (int i = 0; i < adj[cen].size(); i++){
		compressRegion(adj[cen][i], adj[cen][i], cen);
	}
//	for (int i = 0; i < n; i++){
//		printf("%d par %d\n", i, dsu.fpr(i));
//	}
	for (int i = 0, x, y; i < m; i++){
		tie(x, y) = edges[i];
		x = dsu.fpr(x); y = dsu.fpr(y);
		if (x == cen || y == cen) continue;
		if (dsu.merge(x, y)){
			sz[x] += sz[y];
			if (sz[x] >= ord[0].fi){
				work(x, cen);
				return;
			}
		}
	}
}
vi find_split(int N, int A, int B, int C, vi P, vi Q){
	n = N, m = P.size();
	dsu = Dsu(N);
	dsu.init();
	ord.eb(A, 1); ord.eb(B, 2); ord.eb(C, 3);
	sort(ALL(ord));
//	for (int i = 0; i < 3; i++){
//		printf("(%d, %d)%c", ord[i].fi, ord[i].se, " \n"[i==2]);
//	}
	for (int i = 0; i < m; i++){
		g[P[i]].pb(Q[i]);
		g[Q[i]].pb(P[i]);
		edges.eb(P[i], Q[i]);
	}
	Main();
	return res;
}

Compilation message

split.cpp: In function 'void Main()':
split.cpp:196:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < adj[cen].size(); i++){
                  ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 5112 KB ok, correct split
3 Correct 6 ms 4984 KB ok, correct split
4 Correct 6 ms 4984 KB ok, correct split
5 Correct 6 ms 4988 KB ok, correct split
6 Correct 6 ms 5112 KB ok, correct split
7 Correct 153 ms 19068 KB ok, correct split
8 Correct 140 ms 19160 KB ok, correct split
9 Correct 147 ms 19052 KB ok, correct split
10 Correct 148 ms 19052 KB ok, correct split
11 Correct 158 ms 19052 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 4984 KB ok, correct split
3 Correct 8 ms 4984 KB ok, correct split
4 Correct 162 ms 17544 KB ok, correct split
5 Correct 129 ms 16216 KB ok, correct split
6 Correct 179 ms 19064 KB ok, correct split
7 Correct 160 ms 19128 KB ok, correct split
8 Correct 182 ms 19056 KB ok, correct split
9 Correct 186 ms 16180 KB ok, correct split
10 Correct 117 ms 17124 KB ok, correct split
11 Correct 99 ms 17124 KB ok, correct split
12 Correct 102 ms 17252 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 140 ms 16316 KB ok, correct split
3 Correct 45 ms 9456 KB ok, correct split
4 Correct 6 ms 5112 KB ok, correct split
5 Correct 158 ms 17844 KB ok, correct split
6 Correct 153 ms 17828 KB ok, correct split
7 Correct 142 ms 17900 KB ok, correct split
8 Correct 154 ms 17900 KB ok, correct split
9 Correct 164 ms 17900 KB ok, correct split
10 Correct 41 ms 8816 KB ok, no valid answer
11 Correct 61 ms 10628 KB ok, no valid answer
12 Correct 120 ms 16512 KB ok, no valid answer
13 Correct 135 ms 16316 KB ok, no valid answer
14 Correct 104 ms 17252 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB ok, correct split
2 Correct 6 ms 5016 KB ok, no valid answer
3 Correct 6 ms 4984 KB ok, correct split
4 Correct 1 ms 5112 KB ok, correct split
5 Correct 6 ms 5112 KB ok, correct split
6 Correct 6 ms 4984 KB ok, correct split
7 Correct 6 ms 4984 KB ok, correct split
8 Correct 6 ms 4956 KB ok, correct split
9 Correct 9 ms 5368 KB ok, correct split
10 Correct 9 ms 5368 KB ok, correct split
11 Correct 6 ms 4988 KB ok, correct split
12 Correct 9 ms 5360 KB ok, correct split
13 Correct 6 ms 4984 KB ok, correct split
14 Correct 6 ms 4984 KB ok, correct split
15 Correct 6 ms 4984 KB ok, correct split
16 Correct 6 ms 5240 KB ok, correct split
17 Correct 6 ms 4984 KB ok, correct split
18 Correct 6 ms 4988 KB ok, correct split
19 Correct 6 ms 5112 KB ok, correct split
20 Correct 7 ms 5112 KB ok, correct split
21 Correct 8 ms 5372 KB ok, correct split
22 Correct 9 ms 5368 KB ok, correct split
23 Correct 8 ms 5400 KB ok, correct split
24 Correct 8 ms 5368 KB ok, correct split
25 Correct 8 ms 5388 KB ok, correct split
26 Correct 8 ms 5368 KB ok, correct split
27 Correct 10 ms 5368 KB ok, correct split
28 Correct 9 ms 5368 KB ok, correct split
29 Correct 7 ms 5112 KB ok, correct split
30 Correct 9 ms 5368 KB ok, correct split
31 Correct 7 ms 5112 KB ok, correct split
32 Correct 6 ms 5012 KB ok, correct split
33 Correct 6 ms 5112 KB ok, correct split
34 Correct 8 ms 5368 KB ok, correct split
35 Correct 8 ms 5368 KB ok, correct split
36 Correct 8 ms 5240 KB ok, correct split
37 Correct 9 ms 5368 KB ok, correct split
38 Correct 9 ms 5340 KB ok, correct split
39 Correct 9 ms 5368 KB ok, correct split
40 Correct 9 ms 5372 KB ok, correct split
41 Correct 8 ms 5212 KB ok, correct split
42 Correct 7 ms 5240 KB ok, correct split
43 Correct 9 ms 5368 KB ok, correct split
44 Correct 9 ms 5368 KB ok, correct split
45 Correct 9 ms 5368 KB ok, correct split
46 Correct 8 ms 5288 KB ok, correct split
47 Correct 8 ms 5328 KB ok, no valid answer
48 Correct 8 ms 5240 KB ok, correct split
49 Correct 8 ms 5368 KB ok, correct split
50 Correct 8 ms 5064 KB ok, no valid answer
51 Correct 6 ms 4984 KB ok, no valid answer
52 Correct 9 ms 5368 KB ok, no valid answer
53 Correct 9 ms 5368 KB ok, no valid answer
54 Correct 8 ms 5340 KB ok, no valid answer
55 Correct 8 ms 5368 KB ok, no valid answer
56 Correct 9 ms 5368 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB ok, correct split
2 Correct 6 ms 5112 KB ok, correct split
3 Correct 6 ms 4984 KB ok, correct split
4 Correct 6 ms 4984 KB ok, correct split
5 Correct 6 ms 4988 KB ok, correct split
6 Correct 6 ms 5112 KB ok, correct split
7 Correct 153 ms 19068 KB ok, correct split
8 Correct 140 ms 19160 KB ok, correct split
9 Correct 147 ms 19052 KB ok, correct split
10 Correct 148 ms 19052 KB ok, correct split
11 Correct 158 ms 19052 KB ok, correct split
12 Correct 6 ms 4984 KB ok, correct split
13 Correct 6 ms 4984 KB ok, correct split
14 Correct 8 ms 4984 KB ok, correct split
15 Correct 162 ms 17544 KB ok, correct split
16 Correct 129 ms 16216 KB ok, correct split
17 Correct 179 ms 19064 KB ok, correct split
18 Correct 160 ms 19128 KB ok, correct split
19 Correct 182 ms 19056 KB ok, correct split
20 Correct 186 ms 16180 KB ok, correct split
21 Correct 117 ms 17124 KB ok, correct split
22 Correct 99 ms 17124 KB ok, correct split
23 Correct 102 ms 17252 KB ok, correct split
24 Correct 6 ms 4984 KB ok, correct split
25 Correct 140 ms 16316 KB ok, correct split
26 Correct 45 ms 9456 KB ok, correct split
27 Correct 6 ms 5112 KB ok, correct split
28 Correct 158 ms 17844 KB ok, correct split
29 Correct 153 ms 17828 KB ok, correct split
30 Correct 142 ms 17900 KB ok, correct split
31 Correct 154 ms 17900 KB ok, correct split
32 Correct 164 ms 17900 KB ok, correct split
33 Correct 41 ms 8816 KB ok, no valid answer
34 Correct 61 ms 10628 KB ok, no valid answer
35 Correct 120 ms 16512 KB ok, no valid answer
36 Correct 135 ms 16316 KB ok, no valid answer
37 Correct 104 ms 17252 KB ok, no valid answer
38 Correct 6 ms 5112 KB ok, correct split
39 Correct 6 ms 5016 KB ok, no valid answer
40 Correct 6 ms 4984 KB ok, correct split
41 Correct 1 ms 5112 KB ok, correct split
42 Correct 6 ms 5112 KB ok, correct split
43 Correct 6 ms 4984 KB ok, correct split
44 Correct 6 ms 4984 KB ok, correct split
45 Correct 6 ms 4956 KB ok, correct split
46 Correct 9 ms 5368 KB ok, correct split
47 Correct 9 ms 5368 KB ok, correct split
48 Correct 6 ms 4988 KB ok, correct split
49 Correct 9 ms 5360 KB ok, correct split
50 Correct 6 ms 4984 KB ok, correct split
51 Correct 6 ms 4984 KB ok, correct split
52 Correct 6 ms 4984 KB ok, correct split
53 Correct 6 ms 5240 KB ok, correct split
54 Correct 6 ms 4984 KB ok, correct split
55 Correct 6 ms 4988 KB ok, correct split
56 Correct 6 ms 5112 KB ok, correct split
57 Correct 7 ms 5112 KB ok, correct split
58 Correct 8 ms 5372 KB ok, correct split
59 Correct 9 ms 5368 KB ok, correct split
60 Correct 8 ms 5400 KB ok, correct split
61 Correct 8 ms 5368 KB ok, correct split
62 Correct 8 ms 5388 KB ok, correct split
63 Correct 8 ms 5368 KB ok, correct split
64 Correct 10 ms 5368 KB ok, correct split
65 Correct 9 ms 5368 KB ok, correct split
66 Correct 7 ms 5112 KB ok, correct split
67 Correct 9 ms 5368 KB ok, correct split
68 Correct 7 ms 5112 KB ok, correct split
69 Correct 6 ms 5012 KB ok, correct split
70 Correct 6 ms 5112 KB ok, correct split
71 Correct 8 ms 5368 KB ok, correct split
72 Correct 8 ms 5368 KB ok, correct split
73 Correct 8 ms 5240 KB ok, correct split
74 Correct 9 ms 5368 KB ok, correct split
75 Correct 9 ms 5340 KB ok, correct split
76 Correct 9 ms 5368 KB ok, correct split
77 Correct 9 ms 5372 KB ok, correct split
78 Correct 8 ms 5212 KB ok, correct split
79 Correct 7 ms 5240 KB ok, correct split
80 Correct 9 ms 5368 KB ok, correct split
81 Correct 9 ms 5368 KB ok, correct split
82 Correct 9 ms 5368 KB ok, correct split
83 Correct 8 ms 5288 KB ok, correct split
84 Correct 8 ms 5328 KB ok, no valid answer
85 Correct 8 ms 5240 KB ok, correct split
86 Correct 8 ms 5368 KB ok, correct split
87 Correct 8 ms 5064 KB ok, no valid answer
88 Correct 6 ms 4984 KB ok, no valid answer
89 Correct 9 ms 5368 KB ok, no valid answer
90 Correct 9 ms 5368 KB ok, no valid answer
91 Correct 8 ms 5340 KB ok, no valid answer
92 Correct 8 ms 5368 KB ok, no valid answer
93 Correct 9 ms 5368 KB ok, no valid answer
94 Correct 145 ms 16720 KB ok, correct split
95 Correct 182 ms 19184 KB ok, correct split
96 Correct 182 ms 18312 KB ok, correct split
97 Correct 46 ms 9508 KB ok, correct split
98 Correct 48 ms 9712 KB ok, correct split
99 Correct 69 ms 10732 KB ok, correct split
100 Correct 172 ms 19176 KB ok, correct split
101 Correct 169 ms 18152 KB ok, correct split
102 Correct 157 ms 18920 KB ok, correct split
103 Correct 141 ms 18664 KB ok, correct split
104 Correct 156 ms 20176 KB ok, correct split
105 Correct 86 ms 12012 KB ok, correct split
106 Correct 165 ms 20104 KB ok, correct split
107 Correct 152 ms 16292 KB ok, correct split
108 Correct 145 ms 16292 KB ok, correct split
109 Correct 212 ms 19196 KB ok, correct split
110 Correct 179 ms 19572 KB ok, correct split
111 Correct 186 ms 19600 KB ok, correct split
112 Correct 184 ms 19728 KB ok, correct split
113 Correct 175 ms 19688 KB ok, correct split
114 Correct 20 ms 6644 KB ok, correct split
115 Correct 19 ms 6512 KB ok, correct split
116 Correct 164 ms 19568 KB ok, correct split
117 Correct 194 ms 19304 KB ok, correct split
118 Correct 140 ms 16080 KB ok, correct split
119 Correct 165 ms 16108 KB ok, correct split
120 Correct 184 ms 18788 KB ok, correct split
121 Correct 139 ms 16220 KB ok, no valid answer
122 Correct 147 ms 16496 KB ok, no valid answer
123 Correct 197 ms 19120 KB ok, no valid answer
124 Correct 204 ms 19176 KB ok, no valid answer
125 Correct 119 ms 17852 KB ok, no valid answer
126 Correct 92 ms 15980 KB ok, no valid answer
127 Correct 140 ms 18408 KB ok, no valid answer