Submission #144864

# Submission time Handle Problem Language Result Execution time Memory
144864 2019-08-17T23:13:06 Z eriksuenderhauf Split the Attractions (IOI19_split) C++14
100 / 100
521 ms 67944 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "split.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int par[MAXN], sz[MAXN], heavy[MAXN];
int par2[MAXN];
vi adj[MAXN], adj2[MAXN];

int qry(int x) { return par[x] == x ? x : par[x] = qry(par[x]); }
void join(int u, int v) { par[qry(u)] = qry(v); }

vi f(int x, int y, int z) {
	vi ret = {1,2,3};
	vi cur = {x,y,z};
	do {
		if (cur[ret[0]-1] <= cur[ret[1]-1] && cur[ret[1]-1] <= cur[ret[2]-1])
			break;
	} while (next_permutation(ret.begin(),ret.end()));
	return ret;
}

void getSz(int u, int p, int n) {
	sz[u] = 1;
	for (int v: adj[u]) {
		if (v == p)
			continue;
		getSz(v, u, n);
		sz[u] += sz[v];
		if (sz[v] > heavy[u])
			heavy[u] = sz[v];
	}
	if (n - sz[u] > heavy[u])
		heavy[u] = n - sz[u];
	if ((n+1) / 2 < heavy[u])
		heavy[u] = -1;
}

vii inact;

int qry2(int x) { return par2[x] == x ? x : par2[x] = qry2(par2[x]); }
void join2(int u, int v) { par2[qry2(u)] = qry2(v); }

int dfs(int u, int p, int r) {
	join2(u,r);
	int ret = 1;
	for (int v: adj[u])
		if (v != p)
			ret += dfs(v, u, r);
	return ret;
}

void flood(int u, int col, int& cnt, vi& ret, bool fl = false) {
	if (ret[u] || cnt == 0) return;
	cnt--;
	ret[u] = col;
	for (int v: adj2[u])
		if (qry2(v) == qry2(u) || fl)
			flood(v, col, cnt, ret, fl);
}

bool check(int root, int n, vi& ret, vi& col, vi& tmp) {
	for (int i = 0; i < n; i++) par2[i] = i;
	set<pii> childsz;
	set<pii> rev;
	for (int v: adj[root]) {
		int tmpsz = dfs(v, root, v);
		childsz.insert(mp(tmpsz, v));
		rev.insert(mp(v, tmpsz));
	}
	for (int i = 0; i < inact.size() && tmp[0] > (*childsz.rbegin()).fi; i++) {
		int u = qry2(inact[i].fi), v = qry2(inact[i].se);
		if (u == v || u == root || v == root)
			continue;
		auto it1 = *rev.lower_bound(mp(u,-1));
		auto it2 = *rev.lower_bound(mp(v,-1));
		childsz.erase(mp(it1.se,it1.fi));
		childsz.erase(mp(it2.se,it2.fi));
		int nsz = it1.se+it2.se;
		rev.erase(it1);
		rev.erase(it2);
		par2[u] = v;
		childsz.insert(mp(nsz,v));
		rev.insert(mp(v,nsz));
	}
	if (tmp[0] > (*childsz.rbegin()).fi)
		return false;
	int cnt = tmp[0], curcol = col[0];
	flood((*childsz.rbegin()).se, curcol, cnt, ret);
	cnt = tmp[1], curcol = col[1];
	flood(root, curcol, cnt, ret, 1);
	for (int i = 0; i < n; i++)
		if (!ret[i])
			ret[i] = col[2];
	return true;
}

vi find_split(int n, int a, int b, int c, vi p, vi q) {
	for (int i = 0; i < n; i++)
		par[i] = i;
	int m = p.size();
	vi col = f(a,b,c);
	vi tmp = {a,b,c}; sort(tmp.begin(), tmp.end());
	vi ret(n);
	for (int i = 0; i < m; i++) {
		adj2[p[i]].pb(q[i]);
		adj2[q[i]].pb(p[i]);
		if (qry(p[i]) == qry(q[i]))
			inact.pb(mp(p[i],q[i]));
		else {
			join(p[i],q[i]);
			adj[p[i]].pb(q[i]);
			adj[q[i]].pb(p[i]);
		}
	}
	getSz(0, -1, n);
	vi cent;
	int cur = -1;
	for (int i = 0; i < n; i++)
		if (heavy[i] > cur) {
			cur = heavy[i];
			cent = {i};
		} else if (heavy[i] == cur)
			cent.pb(i);
	for (int c: cent)
		if (check(c, n, ret, col, tmp))
			return ret;
	return ret;
}

Compilation message

split.cpp: In function 'bool check(int, int, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
split.cpp:103:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < inact.size() && tmp[0] > (*childsz.rbegin()).fi; i++) {
                  ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB ok, correct split
2 Correct 45 ms 47412 KB ok, correct split
3 Correct 46 ms 47312 KB ok, correct split
4 Correct 46 ms 47352 KB ok, correct split
5 Correct 45 ms 47352 KB ok, correct split
6 Correct 45 ms 47268 KB ok, correct split
7 Correct 234 ms 64676 KB ok, correct split
8 Correct 214 ms 62328 KB ok, correct split
9 Correct 219 ms 61944 KB ok, correct split
10 Correct 205 ms 65012 KB ok, correct split
11 Correct 230 ms 63864 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB ok, correct split
2 Correct 45 ms 47352 KB ok, correct split
3 Correct 45 ms 47352 KB ok, correct split
4 Correct 232 ms 58992 KB ok, correct split
5 Correct 198 ms 57844 KB ok, correct split
6 Correct 210 ms 65012 KB ok, correct split
7 Correct 231 ms 62200 KB ok, correct split
8 Correct 263 ms 60908 KB ok, correct split
9 Correct 217 ms 57712 KB ok, correct split
10 Correct 265 ms 67444 KB ok, correct split
11 Correct 259 ms 67288 KB ok, correct split
12 Correct 269 ms 67728 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB ok, correct split
2 Correct 200 ms 57484 KB ok, correct split
3 Correct 91 ms 51704 KB ok, correct split
4 Correct 44 ms 47244 KB ok, correct split
5 Correct 198 ms 60452 KB ok, correct split
6 Correct 283 ms 60248 KB ok, correct split
7 Correct 211 ms 60284 KB ok, correct split
8 Correct 224 ms 60792 KB ok, correct split
9 Correct 220 ms 60400 KB ok, correct split
10 Correct 84 ms 50808 KB ok, no valid answer
11 Correct 102 ms 52728 KB ok, no valid answer
12 Correct 226 ms 63600 KB ok, no valid answer
13 Correct 212 ms 58816 KB ok, no valid answer
14 Correct 255 ms 67560 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB ok, correct split
2 Correct 45 ms 47352 KB ok, no valid answer
3 Correct 45 ms 47352 KB ok, correct split
4 Correct 44 ms 47356 KB ok, correct split
5 Correct 45 ms 47352 KB ok, correct split
6 Correct 45 ms 47352 KB ok, correct split
7 Correct 45 ms 47284 KB ok, correct split
8 Correct 45 ms 47352 KB ok, correct split
9 Correct 47 ms 47608 KB ok, correct split
10 Correct 48 ms 47608 KB ok, correct split
11 Correct 45 ms 47352 KB ok, correct split
12 Correct 49 ms 47736 KB ok, correct split
13 Correct 45 ms 47352 KB ok, correct split
14 Correct 46 ms 47400 KB ok, correct split
15 Correct 45 ms 47352 KB ok, correct split
16 Correct 45 ms 47280 KB ok, correct split
17 Correct 53 ms 47352 KB ok, correct split
18 Correct 54 ms 47352 KB ok, correct split
19 Correct 62 ms 47352 KB ok, correct split
20 Correct 55 ms 47464 KB ok, correct split
21 Correct 63 ms 47604 KB ok, correct split
22 Correct 47 ms 47600 KB ok, correct split
23 Correct 48 ms 47608 KB ok, correct split
24 Correct 48 ms 47620 KB ok, correct split
25 Correct 57 ms 47608 KB ok, correct split
26 Correct 58 ms 47736 KB ok, correct split
27 Correct 65 ms 47788 KB ok, correct split
28 Correct 49 ms 47740 KB ok, correct split
29 Correct 46 ms 47352 KB ok, correct split
30 Correct 49 ms 47736 KB ok, correct split
31 Correct 46 ms 47352 KB ok, correct split
32 Correct 51 ms 47352 KB ok, correct split
33 Correct 46 ms 47352 KB ok, correct split
34 Correct 48 ms 47692 KB ok, correct split
35 Correct 48 ms 47608 KB ok, correct split
36 Correct 48 ms 47608 KB ok, correct split
37 Correct 51 ms 47868 KB ok, correct split
38 Correct 51 ms 47864 KB ok, correct split
39 Correct 50 ms 47864 KB ok, correct split
40 Correct 50 ms 47864 KB ok, correct split
41 Correct 48 ms 47612 KB ok, correct split
42 Correct 47 ms 47608 KB ok, correct split
43 Correct 50 ms 47776 KB ok, correct split
44 Correct 49 ms 47864 KB ok, correct split
45 Correct 49 ms 47736 KB ok, correct split
46 Correct 48 ms 47740 KB ok, correct split
47 Correct 47 ms 47736 KB ok, no valid answer
48 Correct 48 ms 47608 KB ok, correct split
49 Correct 47 ms 47608 KB ok, correct split
50 Correct 45 ms 47324 KB ok, no valid answer
51 Correct 45 ms 47352 KB ok, no valid answer
52 Correct 48 ms 47608 KB ok, no valid answer
53 Correct 48 ms 47736 KB ok, no valid answer
54 Correct 49 ms 47864 KB ok, no valid answer
55 Correct 49 ms 47736 KB ok, no valid answer
56 Correct 49 ms 47992 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 45 ms 47352 KB ok, correct split
2 Correct 45 ms 47412 KB ok, correct split
3 Correct 46 ms 47312 KB ok, correct split
4 Correct 46 ms 47352 KB ok, correct split
5 Correct 45 ms 47352 KB ok, correct split
6 Correct 45 ms 47268 KB ok, correct split
7 Correct 234 ms 64676 KB ok, correct split
8 Correct 214 ms 62328 KB ok, correct split
9 Correct 219 ms 61944 KB ok, correct split
10 Correct 205 ms 65012 KB ok, correct split
11 Correct 230 ms 63864 KB ok, correct split
12 Correct 45 ms 47352 KB ok, correct split
13 Correct 45 ms 47352 KB ok, correct split
14 Correct 45 ms 47352 KB ok, correct split
15 Correct 232 ms 58992 KB ok, correct split
16 Correct 198 ms 57844 KB ok, correct split
17 Correct 210 ms 65012 KB ok, correct split
18 Correct 231 ms 62200 KB ok, correct split
19 Correct 263 ms 60908 KB ok, correct split
20 Correct 217 ms 57712 KB ok, correct split
21 Correct 265 ms 67444 KB ok, correct split
22 Correct 259 ms 67288 KB ok, correct split
23 Correct 269 ms 67728 KB ok, correct split
24 Correct 45 ms 47352 KB ok, correct split
25 Correct 200 ms 57484 KB ok, correct split
26 Correct 91 ms 51704 KB ok, correct split
27 Correct 44 ms 47244 KB ok, correct split
28 Correct 198 ms 60452 KB ok, correct split
29 Correct 283 ms 60248 KB ok, correct split
30 Correct 211 ms 60284 KB ok, correct split
31 Correct 224 ms 60792 KB ok, correct split
32 Correct 220 ms 60400 KB ok, correct split
33 Correct 84 ms 50808 KB ok, no valid answer
34 Correct 102 ms 52728 KB ok, no valid answer
35 Correct 226 ms 63600 KB ok, no valid answer
36 Correct 212 ms 58816 KB ok, no valid answer
37 Correct 255 ms 67560 KB ok, no valid answer
38 Correct 45 ms 47352 KB ok, correct split
39 Correct 45 ms 47352 KB ok, no valid answer
40 Correct 45 ms 47352 KB ok, correct split
41 Correct 44 ms 47356 KB ok, correct split
42 Correct 45 ms 47352 KB ok, correct split
43 Correct 45 ms 47352 KB ok, correct split
44 Correct 45 ms 47284 KB ok, correct split
45 Correct 45 ms 47352 KB ok, correct split
46 Correct 47 ms 47608 KB ok, correct split
47 Correct 48 ms 47608 KB ok, correct split
48 Correct 45 ms 47352 KB ok, correct split
49 Correct 49 ms 47736 KB ok, correct split
50 Correct 45 ms 47352 KB ok, correct split
51 Correct 46 ms 47400 KB ok, correct split
52 Correct 45 ms 47352 KB ok, correct split
53 Correct 45 ms 47280 KB ok, correct split
54 Correct 53 ms 47352 KB ok, correct split
55 Correct 54 ms 47352 KB ok, correct split
56 Correct 62 ms 47352 KB ok, correct split
57 Correct 55 ms 47464 KB ok, correct split
58 Correct 63 ms 47604 KB ok, correct split
59 Correct 47 ms 47600 KB ok, correct split
60 Correct 48 ms 47608 KB ok, correct split
61 Correct 48 ms 47620 KB ok, correct split
62 Correct 57 ms 47608 KB ok, correct split
63 Correct 58 ms 47736 KB ok, correct split
64 Correct 65 ms 47788 KB ok, correct split
65 Correct 49 ms 47740 KB ok, correct split
66 Correct 46 ms 47352 KB ok, correct split
67 Correct 49 ms 47736 KB ok, correct split
68 Correct 46 ms 47352 KB ok, correct split
69 Correct 51 ms 47352 KB ok, correct split
70 Correct 46 ms 47352 KB ok, correct split
71 Correct 48 ms 47692 KB ok, correct split
72 Correct 48 ms 47608 KB ok, correct split
73 Correct 48 ms 47608 KB ok, correct split
74 Correct 51 ms 47868 KB ok, correct split
75 Correct 51 ms 47864 KB ok, correct split
76 Correct 50 ms 47864 KB ok, correct split
77 Correct 50 ms 47864 KB ok, correct split
78 Correct 48 ms 47612 KB ok, correct split
79 Correct 47 ms 47608 KB ok, correct split
80 Correct 50 ms 47776 KB ok, correct split
81 Correct 49 ms 47864 KB ok, correct split
82 Correct 49 ms 47736 KB ok, correct split
83 Correct 48 ms 47740 KB ok, correct split
84 Correct 47 ms 47736 KB ok, no valid answer
85 Correct 48 ms 47608 KB ok, correct split
86 Correct 47 ms 47608 KB ok, correct split
87 Correct 45 ms 47324 KB ok, no valid answer
88 Correct 45 ms 47352 KB ok, no valid answer
89 Correct 48 ms 47608 KB ok, no valid answer
90 Correct 48 ms 47736 KB ok, no valid answer
91 Correct 49 ms 47864 KB ok, no valid answer
92 Correct 49 ms 47736 KB ok, no valid answer
93 Correct 49 ms 47992 KB ok, no valid answer
94 Correct 239 ms 61296 KB ok, correct split
95 Correct 313 ms 64312 KB ok, correct split
96 Correct 276 ms 63728 KB ok, correct split
97 Correct 96 ms 52088 KB ok, correct split
98 Correct 96 ms 52088 KB ok, correct split
99 Correct 132 ms 54940 KB ok, correct split
100 Correct 269 ms 62276 KB ok, correct split
101 Correct 243 ms 61428 KB ok, correct split
102 Correct 305 ms 66160 KB ok, correct split
103 Correct 316 ms 66424 KB ok, correct split
104 Correct 266 ms 64108 KB ok, correct split
105 Correct 131 ms 54772 KB ok, correct split
106 Correct 328 ms 66652 KB ok, correct split
107 Correct 212 ms 58864 KB ok, correct split
108 Correct 214 ms 58992 KB ok, correct split
109 Correct 286 ms 61820 KB ok, correct split
110 Correct 521 ms 65764 KB ok, correct split
111 Correct 438 ms 65992 KB ok, correct split
112 Correct 412 ms 66280 KB ok, correct split
113 Correct 444 ms 66620 KB ok, correct split
114 Correct 68 ms 49528 KB ok, correct split
115 Correct 78 ms 49272 KB ok, correct split
116 Correct 301 ms 66104 KB ok, correct split
117 Correct 297 ms 65508 KB ok, correct split
118 Correct 219 ms 58736 KB ok, correct split
119 Correct 222 ms 58612 KB ok, correct split
120 Correct 194 ms 61436 KB ok, correct split
121 Correct 226 ms 58968 KB ok, no valid answer
122 Correct 210 ms 61028 KB ok, no valid answer
123 Correct 258 ms 61780 KB ok, no valid answer
124 Correct 267 ms 62428 KB ok, no valid answer
125 Correct 313 ms 67944 KB ok, no valid answer
126 Correct 217 ms 66792 KB ok, no valid answer
127 Correct 363 ms 66664 KB ok, no valid answer