Submission #352653

# Submission time Handle Problem Language Result Execution time Memory
352653 2021-01-21T03:36:11 Z tengiz05 Split the Attractions (IOI19_split) C++17
100 / 100
269 ms 26588 KB
#include "split.h"
#ifndef EVAL
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int N = 1e5+5;
int n, m;
vector<int> edges[N];
vector<int> g[N];
bool used[N];
int subtree_size[N];
vector<pii> extra;
void Dfs(int u, int p=-1){
	used[u] = true;
	for(auto v : edges[u]){
		if(used[v]){
			if(v != p)extra.pb({u,v});
			continue;
		}
		g[u].pb(v);
		g[v].pb(u);
		Dfs(v,u);
	}
}
int find_centroid(int u, int p){
	for(auto v : g[u]){
		if(v!=p && subtree_size[v] > n/2)return find_centroid(v,u);
	}return u;
}
void recalc_size(int u, int p){
	subtree_size[u] = 1;
	for(auto v : g[u]){
		if(v == p)continue;
		recalc_size(v,u);
		subtree_size[u] += subtree_size[v];
	}
}

// start of DSU
vector<int> p, sz;
void Init(int n){
	p.assign(n,0);
	sz.assign(n,1);
	for(int i=0;i<n;i++)p[i]=i;
}
int par(int u){
	if(p[u] == u)return u;
	return p[u] = par(p[u]);
}
bool IsSame(int u, int v){
	return par(u) == par(v);
}
void merge(int u,int v){
	u=par(u);v=par(v);
	if(u == v)return;
	p[v] = u;
	sz[u] += sz[v];
	return;
}
int size_of_node(int u){
	return sz[par(u)];
}// end of DSU

pii sizes[3];
vector<int> ans;
int center;
int remaining;
void color(int u){
	used[u] = true;
	ans[u] = sizes[0].second;
	remaining--;
	if(not remaining)return;
	for(auto v : g[u]){
		if(not remaining)return;
		if(used[v] || v == center)continue;
		color(v);
	}
}

void colorB(int u){
	used[u] = true;
	ans[u] = sizes[1].second;
	remaining--;
	if(not remaining)return;
	for(auto v : edges[u]){
		if(not remaining)return;
		if(used[v])continue;
		colorB(v);
	}
}


vector<int> find_split(int N, int a, int b, int c, vector<int> P, vector<int> Q) {
	Init(N);
	sizes[0] = {a,1};
	sizes[1] = {b,2};
	sizes[2] = {c,3};
	sort(sizes,sizes+3);
	a = sizes[0].first;
	b = sizes[1].first;
	c = sizes[2].first;
	n=N; m = P.size();
	for(int i=0;i<m;i++){
		edges[P[i]].pb(Q[i]);
		edges[Q[i]].pb(P[i]);
	}
	ans.assign(n,sizes[2].second);
	Dfs(0);
	recalc_size(center,center);
	center = find_centroid(0,0);
	recalc_size(center,center);
	for(int u=0;u<n;u++){
		if(u == center)continue;
		for(auto v : g[u]){
			if(v != center)merge(u,v);
		}
	}for(auto u : edges[center]){
		if(size_of_node(u) >= a){
			// yay we found solution! (I hope)
			remaining = a;
			memset(used,0,sizeof used);
			color(u);
			remaining = b;
			colorB(center);
			return ans;
		}
	}
	for(auto [u, v] : extra){
		if(IsSame(u,v))continue;
		if(u == center || v == center)continue;
		g[u].pb(v);
		g[v].pb(u);
		merge(u,v);
		if(size_of_node(u) >= a){
			// yay we found solution! (I hope)
			memset(used,0,sizeof used);
			remaining = a;
			color(u);
			assert(not remaining);
			remaining = b;
			colorB(center);
			assert(not remaining);
			return ans;
		}
	}
	return vector<int>(n,0);
}








# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB ok, correct split
2 Correct 4 ms 5100 KB ok, correct split
3 Correct 4 ms 5100 KB ok, correct split
4 Correct 4 ms 5100 KB ok, correct split
5 Correct 4 ms 5100 KB ok, correct split
6 Correct 4 ms 5100 KB ok, correct split
7 Correct 208 ms 23788 KB ok, correct split
8 Correct 151 ms 21148 KB ok, correct split
9 Correct 162 ms 20204 KB ok, correct split
10 Correct 167 ms 24416 KB ok, correct split
11 Correct 162 ms 24300 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB ok, correct split
2 Correct 4 ms 5100 KB ok, correct split
3 Correct 4 ms 5100 KB ok, correct split
4 Correct 190 ms 21084 KB ok, correct split
5 Correct 139 ms 15164 KB ok, correct split
6 Correct 161 ms 24416 KB ok, correct split
7 Correct 179 ms 20960 KB ok, correct split
8 Correct 210 ms 18408 KB ok, correct split
9 Correct 139 ms 14956 KB ok, correct split
10 Correct 81 ms 15724 KB ok, correct split
11 Correct 77 ms 15716 KB ok, correct split
12 Correct 80 ms 15716 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB ok, correct split
2 Correct 137 ms 15084 KB ok, correct split
3 Correct 51 ms 9196 KB ok, correct split
4 Correct 4 ms 5100 KB ok, correct split
5 Correct 164 ms 18028 KB ok, correct split
6 Correct 160 ms 17772 KB ok, correct split
7 Correct 147 ms 17516 KB ok, correct split
8 Correct 175 ms 19180 KB ok, correct split
9 Correct 183 ms 17248 KB ok, correct split
10 Correct 43 ms 8428 KB ok, no valid answer
11 Correct 53 ms 10092 KB ok, no valid answer
12 Correct 105 ms 15464 KB ok, no valid answer
13 Correct 124 ms 15340 KB ok, no valid answer
14 Correct 81 ms 15672 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB ok, correct split
2 Correct 3 ms 4972 KB ok, no valid answer
3 Correct 3 ms 5100 KB ok, correct split
4 Correct 4 ms 5100 KB ok, correct split
5 Correct 4 ms 5100 KB ok, correct split
6 Correct 4 ms 5100 KB ok, correct split
7 Correct 4 ms 5100 KB ok, correct split
8 Correct 3 ms 5100 KB ok, correct split
9 Correct 6 ms 5740 KB ok, correct split
10 Correct 6 ms 5484 KB ok, correct split
11 Correct 4 ms 5100 KB ok, correct split
12 Correct 7 ms 5484 KB ok, correct split
13 Correct 5 ms 5100 KB ok, correct split
14 Correct 5 ms 5100 KB ok, correct split
15 Correct 3 ms 5100 KB ok, correct split
16 Correct 3 ms 5100 KB ok, correct split
17 Correct 4 ms 5100 KB ok, correct split
18 Correct 4 ms 5100 KB ok, correct split
19 Correct 5 ms 5100 KB ok, correct split
20 Correct 5 ms 5228 KB ok, correct split
21 Correct 5 ms 5484 KB ok, correct split
22 Correct 6 ms 5356 KB ok, correct split
23 Correct 6 ms 5484 KB ok, correct split
24 Correct 6 ms 5484 KB ok, correct split
25 Correct 6 ms 5484 KB ok, correct split
26 Correct 6 ms 5484 KB ok, correct split
27 Correct 6 ms 5484 KB ok, correct split
28 Correct 6 ms 5484 KB ok, correct split
29 Correct 4 ms 5100 KB ok, correct split
30 Correct 7 ms 5484 KB ok, correct split
31 Correct 5 ms 5228 KB ok, correct split
32 Correct 4 ms 5100 KB ok, correct split
33 Correct 5 ms 5100 KB ok, correct split
34 Correct 6 ms 5356 KB ok, correct split
35 Correct 6 ms 5356 KB ok, correct split
36 Correct 6 ms 5356 KB ok, correct split
37 Correct 6 ms 5484 KB ok, correct split
38 Correct 6 ms 5484 KB ok, correct split
39 Correct 6 ms 5484 KB ok, correct split
40 Correct 6 ms 5484 KB ok, correct split
41 Correct 5 ms 5356 KB ok, correct split
42 Correct 5 ms 5356 KB ok, correct split
43 Correct 6 ms 5484 KB ok, correct split
44 Correct 6 ms 5484 KB ok, correct split
45 Correct 6 ms 5484 KB ok, correct split
46 Correct 6 ms 5484 KB ok, correct split
47 Correct 6 ms 5356 KB ok, no valid answer
48 Correct 6 ms 5356 KB ok, correct split
49 Correct 6 ms 5484 KB ok, correct split
50 Correct 4 ms 4972 KB ok, no valid answer
51 Correct 4 ms 4972 KB ok, no valid answer
52 Correct 6 ms 5228 KB ok, no valid answer
53 Correct 6 ms 5356 KB ok, no valid answer
54 Correct 6 ms 5356 KB ok, no valid answer
55 Correct 6 ms 5504 KB ok, no valid answer
56 Correct 6 ms 5356 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB ok, correct split
2 Correct 4 ms 5100 KB ok, correct split
3 Correct 4 ms 5100 KB ok, correct split
4 Correct 4 ms 5100 KB ok, correct split
5 Correct 4 ms 5100 KB ok, correct split
6 Correct 4 ms 5100 KB ok, correct split
7 Correct 208 ms 23788 KB ok, correct split
8 Correct 151 ms 21148 KB ok, correct split
9 Correct 162 ms 20204 KB ok, correct split
10 Correct 167 ms 24416 KB ok, correct split
11 Correct 162 ms 24300 KB ok, correct split
12 Correct 4 ms 5100 KB ok, correct split
13 Correct 4 ms 5100 KB ok, correct split
14 Correct 4 ms 5100 KB ok, correct split
15 Correct 190 ms 21084 KB ok, correct split
16 Correct 139 ms 15164 KB ok, correct split
17 Correct 161 ms 24416 KB ok, correct split
18 Correct 179 ms 20960 KB ok, correct split
19 Correct 210 ms 18408 KB ok, correct split
20 Correct 139 ms 14956 KB ok, correct split
21 Correct 81 ms 15724 KB ok, correct split
22 Correct 77 ms 15716 KB ok, correct split
23 Correct 80 ms 15716 KB ok, correct split
24 Correct 4 ms 5100 KB ok, correct split
25 Correct 137 ms 15084 KB ok, correct split
26 Correct 51 ms 9196 KB ok, correct split
27 Correct 4 ms 5100 KB ok, correct split
28 Correct 164 ms 18028 KB ok, correct split
29 Correct 160 ms 17772 KB ok, correct split
30 Correct 147 ms 17516 KB ok, correct split
31 Correct 175 ms 19180 KB ok, correct split
32 Correct 183 ms 17248 KB ok, correct split
33 Correct 43 ms 8428 KB ok, no valid answer
34 Correct 53 ms 10092 KB ok, no valid answer
35 Correct 105 ms 15464 KB ok, no valid answer
36 Correct 124 ms 15340 KB ok, no valid answer
37 Correct 81 ms 15672 KB ok, no valid answer
38 Correct 4 ms 5100 KB ok, correct split
39 Correct 3 ms 4972 KB ok, no valid answer
40 Correct 3 ms 5100 KB ok, correct split
41 Correct 4 ms 5100 KB ok, correct split
42 Correct 4 ms 5100 KB ok, correct split
43 Correct 4 ms 5100 KB ok, correct split
44 Correct 4 ms 5100 KB ok, correct split
45 Correct 3 ms 5100 KB ok, correct split
46 Correct 6 ms 5740 KB ok, correct split
47 Correct 6 ms 5484 KB ok, correct split
48 Correct 4 ms 5100 KB ok, correct split
49 Correct 7 ms 5484 KB ok, correct split
50 Correct 5 ms 5100 KB ok, correct split
51 Correct 5 ms 5100 KB ok, correct split
52 Correct 3 ms 5100 KB ok, correct split
53 Correct 3 ms 5100 KB ok, correct split
54 Correct 4 ms 5100 KB ok, correct split
55 Correct 4 ms 5100 KB ok, correct split
56 Correct 5 ms 5100 KB ok, correct split
57 Correct 5 ms 5228 KB ok, correct split
58 Correct 5 ms 5484 KB ok, correct split
59 Correct 6 ms 5356 KB ok, correct split
60 Correct 6 ms 5484 KB ok, correct split
61 Correct 6 ms 5484 KB ok, correct split
62 Correct 6 ms 5484 KB ok, correct split
63 Correct 6 ms 5484 KB ok, correct split
64 Correct 6 ms 5484 KB ok, correct split
65 Correct 6 ms 5484 KB ok, correct split
66 Correct 4 ms 5100 KB ok, correct split
67 Correct 7 ms 5484 KB ok, correct split
68 Correct 5 ms 5228 KB ok, correct split
69 Correct 4 ms 5100 KB ok, correct split
70 Correct 5 ms 5100 KB ok, correct split
71 Correct 6 ms 5356 KB ok, correct split
72 Correct 6 ms 5356 KB ok, correct split
73 Correct 6 ms 5356 KB ok, correct split
74 Correct 6 ms 5484 KB ok, correct split
75 Correct 6 ms 5484 KB ok, correct split
76 Correct 6 ms 5484 KB ok, correct split
77 Correct 6 ms 5484 KB ok, correct split
78 Correct 5 ms 5356 KB ok, correct split
79 Correct 5 ms 5356 KB ok, correct split
80 Correct 6 ms 5484 KB ok, correct split
81 Correct 6 ms 5484 KB ok, correct split
82 Correct 6 ms 5484 KB ok, correct split
83 Correct 6 ms 5484 KB ok, correct split
84 Correct 6 ms 5356 KB ok, no valid answer
85 Correct 6 ms 5356 KB ok, correct split
86 Correct 6 ms 5484 KB ok, correct split
87 Correct 4 ms 4972 KB ok, no valid answer
88 Correct 4 ms 4972 KB ok, no valid answer
89 Correct 6 ms 5228 KB ok, no valid answer
90 Correct 6 ms 5356 KB ok, no valid answer
91 Correct 6 ms 5356 KB ok, no valid answer
92 Correct 6 ms 5504 KB ok, no valid answer
93 Correct 6 ms 5356 KB ok, no valid answer
94 Correct 148 ms 19984 KB ok, correct split
95 Correct 206 ms 26588 KB ok, correct split
96 Correct 194 ms 24684 KB ok, correct split
97 Correct 41 ms 9580 KB ok, correct split
98 Correct 49 ms 9836 KB ok, correct split
99 Correct 65 ms 13792 KB ok, correct split
100 Correct 197 ms 21340 KB ok, correct split
101 Correct 169 ms 19940 KB ok, correct split
102 Correct 171 ms 19808 KB ok, correct split
103 Correct 137 ms 19168 KB ok, correct split
104 Correct 142 ms 21708 KB ok, correct split
105 Correct 72 ms 12140 KB ok, correct split
106 Correct 164 ms 21836 KB ok, correct split
107 Correct 127 ms 16364 KB ok, correct split
108 Correct 130 ms 16236 KB ok, correct split
109 Correct 210 ms 20708 KB ok, correct split
110 Correct 180 ms 20828 KB ok, correct split
111 Correct 187 ms 20828 KB ok, correct split
112 Correct 175 ms 21084 KB ok, correct split
113 Correct 168 ms 21084 KB ok, correct split
114 Correct 17 ms 7144 KB ok, correct split
115 Correct 19 ms 6888 KB ok, correct split
116 Correct 172 ms 20700 KB ok, correct split
117 Correct 168 ms 20628 KB ok, correct split
118 Correct 142 ms 16176 KB ok, correct split
119 Correct 128 ms 16108 KB ok, correct split
120 Correct 158 ms 21100 KB ok, correct split
121 Correct 158 ms 16236 KB ok, no valid answer
122 Correct 117 ms 16364 KB ok, no valid answer
123 Correct 244 ms 21608 KB ok, no valid answer
124 Correct 269 ms 21484 KB ok, no valid answer
125 Correct 144 ms 18148 KB ok, no valid answer
126 Correct 68 ms 15716 KB ok, no valid answer
127 Correct 154 ms 18784 KB ok, no valid answer