Submission #737158

# Submission time Handle Problem Language Result Execution time Memory
737158 2023-05-06T17:32:16 Z penguin133 Village (BOI20_village) C++17
56 / 100
236 ms 64216 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int S[200005], cnt = 1, E[200005], p[20][200005];
vector <int> adj[200005];
int n, dep[200005], sz[200005], sz2[200005];
int mp[200006];

void dfs(int x, int par, int d){
	S[x] = cnt++;
	p[0][x] = par;
	dep[x] = d;
	sz[x] = 1, sz2[x] = d;
	for(auto i : adj[x]){
		if(i == par)continue;
		dfs(i, x, d + 1);
		sz[x] += sz[i];
		sz2[x] += sz2[i];
	}
	E[x] = cnt - 1;
}

int lca(int u, int v){
	if(dep[u] > dep[v])swap(u, v);
	int diff = dep[v] - dep[u];
	for(int i=19;i>=0;i--)if(diff >> i & 1)v = p[i][v];
	if(u == v)return u;
	for(int i=19;i>=0;i--){
		if(p[i][u] != p[i][v])u = p[i][u], v = p[i][v];
	}
	return p[0][u];
}
int hv[200005], ans, bru[200005], f = 0;
void grr(int x, int par){
	vector <int> tmp;
	for(auto i : adj[x]){
		if(i == par)continue;
		grr(i, x);
		if(hv[i])tmp.push_back(i);
	}
	bool g = 1;
	if((int)tmp.size() >= 2 && (int)tmp.size()%2 == 0){
		int xx = tmp.back(); tmp.pop_back();
		int y = tmp.back(); tmp.pop_back();
		ans += 2;
		bru[x] = xx;
		bru[xx] = y;
		bru[y] = x;
		f = 1;
		g = 0;
	}
	while((int)tmp.size() >= 2){
		int xx = tmp.back(); tmp.pop_back();
		int y = tmp.back(); tmp.pop_back();
		ans += 2;
		bru[xx] = y, bru[y] = xx;
	}
	if(tmp.empty())hv[x] = g;
	else ans++, bru[x] = tmp[0], bru[tmp[0]] = x;
}

struct node{
	int s, e, m, val, pos, val2;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		val = 0, pos = s;
		val2 = e - s + 1;
		if(s != e)l = new node(s, m), r = new node(m+1, e);
	}
	
	void upd(int p, int v){
		if(s == e){
			val = v;
			if(v == -1)val2--;
		}
		else{
			if(p <= m)l->upd(p, v);
			else r->upd(p, v);
			val = max(l->val, r->val);
			if(l->val >= r->val)pos = l->pos;
			else pos = r->pos;
			val2 = l->val2 + r->val2;
		}
	}
	
	pi qry(int a, int b){
		if(s == a && e == b)return {val, pos};
		else if(b <= m)return l->qry(a, b);
		else if(a > m)return r->qry(a, b);
		else return max(l->qry(a, m), r->qry(m+1, b));
	}
	
	int qr(int a, int b){
		if(a == s && b == e)return val2;
		else if(b <= m)return l->qr(a, b);
		else if(a > m)return r->qr(a, b);
		else return l->qr(a, m) + r->qr(m+1, b);
	}
	
}*root;

int bru2[200005], ans2,  ded[200005];
void grr2(int xx, int par){
	priority_queue <pi> pq;
	for(auto i : adj[xx]){
		if(i == par)continue;
		if(root->qr(S[i], E[i]))pq.push({root->qr(S[i], E[i]), i});
	}
	while((int)pq.size() > 1){
		pi x = pq.top(); pq.pop();
		pi y = pq.top(); pq.pop();
		pi mx = root->qry(S[x.se], E[x.se]);
		pi mx2 = root->qry(S[y.se], E[y.se]);
		ans2 += mx.fi + mx2.fi - 2 * dep[xx];
		root->upd(mx.se, -1);
		root->upd(mx2.se, -1);
		mx.se = mp[mx.se];
		mx2.se = mp[mx2.se];
		bru2[mx.se] = mx2.se;
		bru2[mx2.se] = mx.se;
		ded[mx.se] = ded[mx2.se] = 1;
		sz[x.se]--;
		sz[y.se]--;
		x.fi--;
		y.fi--;
		if(x.fi)pq.push(x);
		if(y.fi)pq.push(y);
	}
	if(!pq.empty() && !ded[xx]){
		pi x = pq.top(); pq.pop();
		pi mx = root->qry(S[x.se], E[x.se]);
		ans2 += mx.fi - dep[xx];
		root->upd(mx.se, -1);
		mx.se = mp[mx.se];
		sz[x.se]--;
		bru2[xx] = mx.se;
		bru2[mx.se] = xx;
		ded[mx.se] = ded[xx] = 1;
	}
	for(auto i : adj[xx]){
		if(i == par)continue;
		if(root->qr(S[i], E[i]))grr2(i, xx);
	}
}

int B[500005];
void solve(){
	cin >> n;
	for(int i=1;i<n;i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1, 1);
	for(int i=1;i<=19;i++)for(int j=1;j<=n;j++)p[i][j] = p[i-1][p[i-1][j]];
	grr(1, -1);
	root = new node(1, n);
	for(int i=1;i<=n;i++)mp[S[i]] = i;
	for(int i=1;i<=n;i++)root->upd(S[i], dep[i]);
	if(hv[1]){
		int x = adj[1][0];
		int y = bru[x];
		if(bru[y] == x){
			
			ans++;
			bru[1] = x, bru[x] = y, bru[y] = 1;
		}
		else{
			int z = bru[y];
			bru[z] = y;
			bru[1] = x; bru[x] = 1;
			ans++;
		}
	}
	grr2(1, -1);
	for(int i=1;i<=n;i++){
		if(!bru2[i]){
			int x = (i == n ? i - 1 : i + 1);
			int y = bru2[x];
			bru2[i] = x, bru2[x] = y, bru2[y] = i;
		}
	}
	cout << ans * 2 << ' ' << ans2 * 2 << '\n';
	for(int i=1;i<=n;i++)cout << bru[i] << ' ';
	cout << '\n';
	for(int i=1;i<=n;i++)cout << bru2[i] << ' ';
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

Village.cpp:200:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  200 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 2 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 3 ms 5460 KB Output is correct
3 Partially correct 3 ms 5460 KB Partially correct
4 Partially correct 3 ms 5588 KB Partially correct
5 Partially correct 4 ms 5588 KB Partially correct
6 Correct 5 ms 5588 KB Output is correct
7 Correct 4 ms 5840 KB Output is correct
8 Correct 4 ms 5588 KB Output is correct
9 Partially correct 3 ms 5588 KB Partially correct
10 Correct 4 ms 5588 KB Output is correct
11 Correct 4 ms 5716 KB Output is correct
12 Correct 4 ms 5588 KB Output is correct
13 Correct 4 ms 5588 KB Output is correct
14 Correct 4 ms 5588 KB Output is correct
15 Correct 3 ms 5588 KB Output is correct
16 Correct 4 ms 5588 KB Output is correct
17 Correct 4 ms 5588 KB Output is correct
18 Correct 3 ms 5588 KB Output is correct
19 Correct 4 ms 5588 KB Output is correct
20 Correct 4 ms 5588 KB Output is correct
21 Correct 4 ms 5588 KB Output is correct
22 Correct 5 ms 5680 KB Output is correct
23 Correct 3 ms 5600 KB Output is correct
24 Partially correct 3 ms 5588 KB Partially correct
25 Correct 3 ms 5460 KB Output is correct
26 Partially correct 4 ms 5588 KB Partially correct
27 Correct 3 ms 5460 KB Output is correct
28 Correct 4 ms 5716 KB Output is correct
29 Correct 4 ms 5588 KB Output is correct
30 Correct 4 ms 5588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 3 ms 5204 KB Output is correct
4 Correct 2 ms 5204 KB Output is correct
5 Correct 3 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 3 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Correct 2 ms 5204 KB Output is correct
12 Correct 3 ms 5204 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 3 ms 5204 KB Output is correct
18 Correct 3 ms 5332 KB Output is correct
19 Correct 3 ms 5460 KB Output is correct
20 Partially correct 3 ms 5460 KB Partially correct
21 Partially correct 3 ms 5588 KB Partially correct
22 Partially correct 4 ms 5588 KB Partially correct
23 Correct 5 ms 5588 KB Output is correct
24 Correct 4 ms 5840 KB Output is correct
25 Correct 4 ms 5588 KB Output is correct
26 Partially correct 3 ms 5588 KB Partially correct
27 Correct 4 ms 5588 KB Output is correct
28 Correct 4 ms 5716 KB Output is correct
29 Correct 4 ms 5588 KB Output is correct
30 Correct 4 ms 5588 KB Output is correct
31 Correct 4 ms 5588 KB Output is correct
32 Correct 3 ms 5588 KB Output is correct
33 Correct 4 ms 5588 KB Output is correct
34 Correct 4 ms 5588 KB Output is correct
35 Correct 3 ms 5588 KB Output is correct
36 Correct 4 ms 5588 KB Output is correct
37 Correct 4 ms 5588 KB Output is correct
38 Correct 4 ms 5588 KB Output is correct
39 Correct 5 ms 5680 KB Output is correct
40 Correct 3 ms 5600 KB Output is correct
41 Partially correct 3 ms 5588 KB Partially correct
42 Correct 3 ms 5460 KB Output is correct
43 Partially correct 4 ms 5588 KB Partially correct
44 Correct 3 ms 5460 KB Output is correct
45 Correct 4 ms 5716 KB Output is correct
46 Correct 4 ms 5588 KB Output is correct
47 Correct 4 ms 5588 KB Output is correct
48 Partially correct 192 ms 45256 KB Partially correct
49 Partially correct 183 ms 49208 KB Partially correct
50 Partially correct 182 ms 49208 KB Partially correct
51 Partially correct 132 ms 39408 KB Partially correct
52 Partially correct 183 ms 49092 KB Partially correct
53 Partially correct 180 ms 44748 KB Partially correct
54 Correct 81 ms 33648 KB Output is correct
55 Correct 236 ms 64216 KB Output is correct
56 Correct 230 ms 56600 KB Output is correct
57 Correct 203 ms 54004 KB Output is correct
58 Correct 196 ms 52276 KB Output is correct
59 Partially correct 186 ms 49884 KB Partially correct
60 Correct 207 ms 50348 KB Output is correct
61 Correct 186 ms 50104 KB Output is correct
62 Correct 166 ms 50080 KB Output is correct
63 Correct 160 ms 47432 KB Output is correct
64 Partially correct 175 ms 50236 KB Partially correct
65 Correct 189 ms 50116 KB Output is correct
66 Correct 155 ms 47312 KB Output is correct
67 Correct 136 ms 38224 KB Output is correct
68 Correct 132 ms 43184 KB Output is correct
69 Correct 164 ms 50276 KB Output is correct
70 Correct 159 ms 47436 KB Output is correct
71 Correct 111 ms 36460 KB Output is correct
72 Correct 124 ms 40824 KB Output is correct
73 Correct 228 ms 50424 KB Output is correct
74 Correct 158 ms 46460 KB Output is correct
75 Partially correct 175 ms 50288 KB Partially correct
76 Partially correct 194 ms 49632 KB Partially correct
77 Correct 193 ms 47756 KB Output is correct
78 Correct 119 ms 34996 KB Output is correct
79 Correct 152 ms 39856 KB Output is correct
80 Partially correct 177 ms 50172 KB Partially correct
81 Correct 159 ms 47948 KB Output is correct
82 Correct 180 ms 49584 KB Output is correct