Submission #144769

# Submission time Handle Problem Language Result Execution time Memory
144769 2019-08-17T16:23:32 Z abeker Split the Attractions (IOI19_split) C++17
100 / 100
420 ms 72868 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair <int, int> pii;

struct tree {
	int n;
	vector <int> weight, dad;
	vector <vector <pii>> adj;
	
	tree() {
		n = 0;
	}
	
	int add_node() {
		weight.push_back(0);
		dad.push_back(-1);
		adj.push_back({});
		return n++;
	}
	
	void add_edge(int x, int y) {
		adj[x].push_back({y, 0});
		adj[y].push_back({x, 0});
	}
	
	void set_weight(int x, int w) {
		weight[x] = w;
	}
	
	void calc(int x, int p) {
		dad[x] = p;
		for (auto &it : adj[x]) 
			if (it.first != p) {
				calc(it.first, x);
				weight[x] += weight[it.first];
				it.second = weight[it.first];
			}
	}
	
	int centroid() {
		for (int i = 0; i < n; i++) {
			int mx = 0;
			for (auto &it : adj[i]) {
				if (it.first == dad[i])
					it.second = weight[0] - weight[i];
				mx = max(mx, it.second);
			}
			if (mx <= weight[0] / 2)
				return i;
		}
		assert(false);
	}
	
	void go(int x, int p, vector <int> &v) {
		v.push_back(x);
		for (auto it : adj[x])
			if (it.first != p)
				go(it.first, x, v);
	}
};

struct graph {
	int n, timer;
	vector <vector <int>> adj;
	vector <int> disc, low;
	vector <int> dad, link;
	vector <vector <int>> comps;
	vector <vector <int>> memo, group;
	vector <int> preorder;
	stack <int> stk;
	vector <bool> art;
	vector <int> idx;
	
	graph(int _n) {
		n = _n;
		timer = 0;
		disc.resize(n);
		low.resize(n);
		dad.resize(n);
		link.resize(n);
		art.resize(n);
		idx.resize(n);
		adj.resize(n);
		memo.resize(2 * n);
		group.resize(n);
		for (int i = 0; i < n; i++)
			group[i] = {i};
	}
	
	graph(){}
	
	void add_edge(int x, int y) {
		adj[x].push_back(y);
		adj[y].push_back(x);
	}
	
	void dfs(int x, int p) {
		preorder.push_back(x);
		disc[x] = low[x] = ++timer;
		link[x] = x;
		dad[x] = p;
		stk.push(x);
		for (auto it : adj[x])
			if (!disc[it]) {
				dfs(it, x);
				if (low[it] < low[x]) {
					low[x] = low[it];
					link[x] = link[it];
				}
				if (low[it] >= disc[x]) {
					art[x] = disc[x] > 1 || disc[it] > 2;
					comps.push_back({x});
					for (; comps.back().back() != it; stk.pop())
						comps.back().push_back(stk.top());
				}
			}
			else if (it != p && disc[it] < low[x]) {
				low[x] = disc[it];
				link[x] = it;
			}
	}
	
	tree block_cut() {
		tree res;
		for (int i = 0; i < n; i++)
			if (art[i]) {
				idx[i] = res.add_node();
				res.set_weight(idx[i], 1);
				memo[idx[i]] = {i};
			}
		
		for (auto comp : comps) {
			int node = res.add_node();
			int sz = comp.size();
			memo[node] = comp;
			for (auto it : comp)
				if (art[it]) {
					res.add_edge(node, idx[it]);
					sz--;
				}
			res.set_weight(node, sz);
		}
		
		return res;
	}
	
	vector <int> st_numbering() {
		vector <int> prv(n, -1), nxt(n, -1), sgn(n, -1);
		int s = preorder[0];
		int t = preorder[1];
		sgn[s] = 0;
		nxt[s] = t;
		prv[t] = s;
		for (auto it : preorder) {
			if (disc[it] <= 2)
				continue;
			if (sgn[link[it]]) {
				sgn[dad[it]] = 0;
				if (nxt[dad[it]] != -1)
					prv[nxt[dad[it]]] = it;
				nxt[it] = nxt[dad[it]];
				prv[it] = dad[it];
				nxt[dad[it]] = it;
			}
			else {
				sgn[dad[it]] = 1;
				if (prv[dad[it]] != -1)
					nxt[prv[dad[it]]] = it;
				prv[it] = prv[dad[it]];
				nxt[it] = dad[it];
				prv[dad[it]] = it;
			}
		}
		
		vector <int> res;
		for (int x = s; x != -1; x = nxt[x])
			res.push_back(x);
		
		return res;
	}
	
	graph induced(const vector <int> &subset) {
		vector <int> label(n, -1);
		int sz = subset.size();
		for (int i = 0; i < sz; i++)
			label[subset[i]] = i;
		
		graph res(sz);
		for (int i = 0; i < sz; i++)
			for (auto it : adj[subset[i]])
				if (label[it] != -1 && i < label[it]) 
					res.add_edge(i, label[it]);
		
		return res;
	}
	
	vector <int> clip(int num) {
		dfs(0, -1);
		preorder.resize(num);
		return preorder;
	}
	
	vector <int> construct(const vector <int> &subset, const vector <pii> &p) {
		vector <bool> in(n, false);
		for (auto it : subset)
			in[it] = true;
		
		vector <int> rest;
		for (int i = 0; i < n; i++)
			if (!in[i])
				rest.push_back(i);
		
		vector <int> A = induced(subset).clip(p[0].first);
		vector <int> B = induced(rest).clip(p[1].first);
		
		vector <int> res(n, p[2].second);
		for (auto it : A)	
			res[subset[it]] = p[0].second;
		for (auto it : B)
			res[rest[it]] = p[1].second;
		
		return res;
	}
};

graph G, H;
tree T;

vector <int> find_split(int N, int a, int b, int c, vector <int> u, vector <int> v) {
	vector <pii> parts = {{a, 1}, {b, 2}, {c, 3}};
	sort(parts.begin(), parts.end());
	
	G = graph(N);
	for (int i = 0; i < u.size(); i++)
		G.add_edge(u[i], v[i]);
	
	G.dfs(0, -1);
	
	T = G.block_cut();
	T.calc(0, -1);
	int node = T.centroid();
	
	bool single = G.memo[node].size() == 1;
	for (auto it : T.adj[node]) {
		vector <int> branch;
		T.go(it.first, node, branch);
		vector <int> tmp;
		for (auto it1 : branch)
			for (auto it2 : G.memo[it1])
				if (!single || it2 != G.memo[node][0]) 
					tmp.push_back(it2);
		sort(tmp.begin(), tmp.end());
		tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin());
		assert(tmp.size() == it.second);
		G.group[G.memo[it.first][0]] = tmp;
		if (it.second >= parts[0].first)
			return G.construct(tmp, parts);
	}
	
	if (single)
		return vector <int> (N, 0);
	
	H = G.induced(G.memo[node]);
	H.dfs(0, -1);
	vector <int> order = H.st_numbering();
	vector <int> lft;
	for (auto it : order) {
		int tmp = G.memo[node][it];
		for (auto it1 : G.group[tmp])
			lft.push_back(it1);
		if (lft.size() >= parts[0].first)
			return G.construct(lft, parts);
	}
	
	assert(false);
}

Compilation message

split.cpp: In function 'std::vector<int> find_split(int, int, int, int, std::vector<int>, std::vector<int>)':
split.cpp:235:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < u.size(); i++)
                  ~~^~~~~~~~~~
In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from split.cpp:1:
split.cpp:255:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   assert(tmp.size() == it.second);
          ~~~~~~~~~~~^~~~~~~
split.cpp:272:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (lft.size() >= parts[0].first)
       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 2 ms 376 KB ok, correct split
3 Correct 2 ms 256 KB ok, correct split
4 Correct 2 ms 256 KB ok, correct split
5 Correct 2 ms 380 KB ok, correct split
6 Correct 2 ms 376 KB ok, correct split
7 Correct 349 ms 67272 KB ok, correct split
8 Correct 325 ms 64024 KB ok, correct split
9 Correct 331 ms 63212 KB ok, correct split
10 Correct 274 ms 72868 KB ok, correct split
11 Correct 263 ms 66884 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 2 ms 376 KB ok, correct split
3 Correct 2 ms 256 KB ok, correct split
4 Correct 263 ms 52092 KB ok, correct split
5 Correct 302 ms 64120 KB ok, correct split
6 Correct 278 ms 72864 KB ok, correct split
7 Correct 420 ms 63920 KB ok, correct split
8 Correct 283 ms 45352 KB ok, correct split
9 Correct 188 ms 33964 KB ok, correct split
10 Correct 206 ms 61408 KB ok, correct split
11 Correct 200 ms 61284 KB ok, correct split
12 Correct 188 ms 61288 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 326 ms 60964 KB ok, correct split
3 Correct 109 ms 26284 KB ok, correct split
4 Correct 2 ms 376 KB ok, correct split
5 Correct 364 ms 66424 KB ok, correct split
6 Correct 336 ms 66084 KB ok, correct split
7 Correct 370 ms 66800 KB ok, correct split
8 Correct 359 ms 67716 KB ok, correct split
9 Correct 360 ms 65796 KB ok, correct split
10 Correct 66 ms 13740 KB ok, no valid answer
11 Correct 103 ms 21292 KB ok, no valid answer
12 Correct 179 ms 38176 KB ok, no valid answer
13 Correct 242 ms 42292 KB ok, no valid answer
14 Correct 143 ms 36672 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 2 ms 256 KB ok, no valid answer
3 Correct 2 ms 256 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 2 ms 256 KB ok, correct split
8 Correct 2 ms 256 KB ok, correct split
9 Correct 6 ms 1112 KB ok, correct split
10 Correct 7 ms 1404 KB ok, correct split
11 Correct 2 ms 376 KB ok, correct split
12 Correct 7 ms 1540 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 376 KB ok, correct split
16 Correct 2 ms 376 KB ok, correct split
17 Correct 2 ms 376 KB ok, correct split
18 Correct 2 ms 376 KB ok, correct split
19 Correct 3 ms 632 KB ok, correct split
20 Correct 4 ms 888 KB ok, correct split
21 Correct 8 ms 1912 KB ok, correct split
22 Correct 8 ms 1912 KB ok, correct split
23 Correct 8 ms 2040 KB ok, correct split
24 Correct 8 ms 1912 KB ok, correct split
25 Correct 8 ms 2040 KB ok, correct split
26 Correct 7 ms 1784 KB ok, correct split
27 Correct 7 ms 1784 KB ok, correct split
28 Correct 7 ms 1656 KB ok, correct split
29 Correct 2 ms 504 KB ok, correct split
30 Correct 6 ms 1272 KB ok, correct split
31 Correct 3 ms 504 KB ok, correct split
32 Correct 3 ms 504 KB ok, correct split
33 Correct 3 ms 632 KB ok, correct split
34 Correct 8 ms 1784 KB ok, correct split
35 Correct 8 ms 1656 KB ok, correct split
36 Correct 7 ms 1656 KB ok, correct split
37 Correct 8 ms 1784 KB ok, correct split
38 Correct 8 ms 1784 KB ok, correct split
39 Correct 8 ms 1784 KB ok, correct split
40 Correct 8 ms 1784 KB ok, correct split
41 Correct 5 ms 1016 KB ok, correct split
42 Correct 5 ms 1016 KB ok, correct split
43 Correct 6 ms 1400 KB ok, correct split
44 Correct 6 ms 1400 KB ok, correct split
45 Correct 6 ms 1400 KB ok, correct split
46 Correct 5 ms 1272 KB ok, correct split
47 Correct 4 ms 1016 KB ok, no valid answer
48 Correct 5 ms 1400 KB ok, correct split
49 Correct 5 ms 1272 KB ok, correct split
50 Correct 2 ms 256 KB ok, no valid answer
51 Correct 2 ms 376 KB ok, no valid answer
52 Correct 6 ms 1272 KB ok, no valid answer
53 Correct 5 ms 1016 KB ok, no valid answer
54 Correct 5 ms 1272 KB ok, no valid answer
55 Correct 5 ms 1016 KB ok, no valid answer
56 Correct 5 ms 1144 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB ok, correct split
2 Correct 2 ms 376 KB ok, correct split
3 Correct 2 ms 256 KB ok, correct split
4 Correct 2 ms 256 KB ok, correct split
5 Correct 2 ms 380 KB ok, correct split
6 Correct 2 ms 376 KB ok, correct split
7 Correct 349 ms 67272 KB ok, correct split
8 Correct 325 ms 64024 KB ok, correct split
9 Correct 331 ms 63212 KB ok, correct split
10 Correct 274 ms 72868 KB ok, correct split
11 Correct 263 ms 66884 KB ok, correct split
12 Correct 2 ms 256 KB ok, correct split
13 Correct 2 ms 376 KB ok, correct split
14 Correct 2 ms 256 KB ok, correct split
15 Correct 263 ms 52092 KB ok, correct split
16 Correct 302 ms 64120 KB ok, correct split
17 Correct 278 ms 72864 KB ok, correct split
18 Correct 420 ms 63920 KB ok, correct split
19 Correct 283 ms 45352 KB ok, correct split
20 Correct 188 ms 33964 KB ok, correct split
21 Correct 206 ms 61408 KB ok, correct split
22 Correct 200 ms 61284 KB ok, correct split
23 Correct 188 ms 61288 KB ok, correct split
24 Correct 2 ms 256 KB ok, correct split
25 Correct 326 ms 60964 KB ok, correct split
26 Correct 109 ms 26284 KB ok, correct split
27 Correct 2 ms 376 KB ok, correct split
28 Correct 364 ms 66424 KB ok, correct split
29 Correct 336 ms 66084 KB ok, correct split
30 Correct 370 ms 66800 KB ok, correct split
31 Correct 359 ms 67716 KB ok, correct split
32 Correct 360 ms 65796 KB ok, correct split
33 Correct 66 ms 13740 KB ok, no valid answer
34 Correct 103 ms 21292 KB ok, no valid answer
35 Correct 179 ms 38176 KB ok, no valid answer
36 Correct 242 ms 42292 KB ok, no valid answer
37 Correct 143 ms 36672 KB ok, no valid answer
38 Correct 2 ms 256 KB ok, correct split
39 Correct 2 ms 256 KB ok, no valid answer
40 Correct 2 ms 256 KB ok, correct split
41 Correct 2 ms 376 KB ok, correct split
42 Correct 2 ms 256 KB ok, correct split
43 Correct 2 ms 256 KB ok, correct split
44 Correct 2 ms 256 KB ok, correct split
45 Correct 2 ms 256 KB ok, correct split
46 Correct 6 ms 1112 KB ok, correct split
47 Correct 7 ms 1404 KB ok, correct split
48 Correct 2 ms 376 KB ok, correct split
49 Correct 7 ms 1540 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 376 KB ok, correct split
53 Correct 2 ms 376 KB ok, correct split
54 Correct 2 ms 376 KB ok, correct split
55 Correct 2 ms 376 KB ok, correct split
56 Correct 3 ms 632 KB ok, correct split
57 Correct 4 ms 888 KB ok, correct split
58 Correct 8 ms 1912 KB ok, correct split
59 Correct 8 ms 1912 KB ok, correct split
60 Correct 8 ms 2040 KB ok, correct split
61 Correct 8 ms 1912 KB ok, correct split
62 Correct 8 ms 2040 KB ok, correct split
63 Correct 7 ms 1784 KB ok, correct split
64 Correct 7 ms 1784 KB ok, correct split
65 Correct 7 ms 1656 KB ok, correct split
66 Correct 2 ms 504 KB ok, correct split
67 Correct 6 ms 1272 KB ok, correct split
68 Correct 3 ms 504 KB ok, correct split
69 Correct 3 ms 504 KB ok, correct split
70 Correct 3 ms 632 KB ok, correct split
71 Correct 8 ms 1784 KB ok, correct split
72 Correct 8 ms 1656 KB ok, correct split
73 Correct 7 ms 1656 KB ok, correct split
74 Correct 8 ms 1784 KB ok, correct split
75 Correct 8 ms 1784 KB ok, correct split
76 Correct 8 ms 1784 KB ok, correct split
77 Correct 8 ms 1784 KB ok, correct split
78 Correct 5 ms 1016 KB ok, correct split
79 Correct 5 ms 1016 KB ok, correct split
80 Correct 6 ms 1400 KB ok, correct split
81 Correct 6 ms 1400 KB ok, correct split
82 Correct 6 ms 1400 KB ok, correct split
83 Correct 5 ms 1272 KB ok, correct split
84 Correct 4 ms 1016 KB ok, no valid answer
85 Correct 5 ms 1400 KB ok, correct split
86 Correct 5 ms 1272 KB ok, correct split
87 Correct 2 ms 256 KB ok, no valid answer
88 Correct 2 ms 376 KB ok, no valid answer
89 Correct 6 ms 1272 KB ok, no valid answer
90 Correct 5 ms 1016 KB ok, no valid answer
91 Correct 5 ms 1272 KB ok, no valid answer
92 Correct 5 ms 1016 KB ok, no valid answer
93 Correct 5 ms 1144 KB ok, no valid answer
94 Correct 329 ms 62496 KB ok, correct split
95 Correct 383 ms 62856 KB ok, correct split
96 Correct 351 ms 61696 KB ok, correct split
97 Correct 105 ms 26540 KB ok, correct split
98 Correct 96 ms 22496 KB ok, correct split
99 Correct 91 ms 14160 KB ok, correct split
100 Correct 288 ms 44972 KB ok, correct split
101 Correct 249 ms 45736 KB ok, correct split
102 Correct 236 ms 47468 KB ok, correct split
103 Correct 265 ms 49808 KB ok, correct split
104 Correct 200 ms 37192 KB ok, correct split
105 Correct 188 ms 35484 KB ok, correct split
106 Correct 251 ms 61256 KB ok, correct split
107 Correct 322 ms 58900 KB ok, correct split
108 Correct 316 ms 58532 KB ok, correct split
109 Correct 276 ms 39572 KB ok, correct split
110 Correct 308 ms 55596 KB ok, correct split
111 Correct 327 ms 60252 KB ok, correct split
112 Correct 315 ms 56360 KB ok, correct split
113 Correct 324 ms 60540 KB ok, correct split
114 Correct 29 ms 6488 KB ok, correct split
115 Correct 27 ms 6232 KB ok, correct split
116 Correct 237 ms 42204 KB ok, correct split
117 Correct 234 ms 41140 KB ok, correct split
118 Correct 184 ms 31428 KB ok, correct split
119 Correct 192 ms 40044 KB ok, correct split
120 Correct 231 ms 39524 KB ok, correct split
121 Correct 278 ms 37796 KB ok, no valid answer
122 Correct 238 ms 42688 KB ok, no valid answer
123 Correct 206 ms 26220 KB ok, no valid answer
124 Correct 183 ms 26024 KB ok, no valid answer
125 Correct 158 ms 33624 KB ok, no valid answer
126 Correct 128 ms 33112 KB ok, no valid answer
127 Correct 157 ms 29344 KB ok, no valid answer