답안 #737153

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
737153 2023-05-06T17:29:50 Z penguin133 Village (BOI20_village) C++17
56 / 100
271 ms 77768 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->qry(S[i], E[i]).fi > -1)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;
		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;
			ans2 += (dep[x] + dep[i] - 2 * dep[lca(x, i)] + dep[i] + dep[y] - 2 * dep[lca(i, y)] - dep[x] - dep[y] + 2 * dep[lca(x, y)]) / 2;
		}
	}
	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:201:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  201 | main(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5164 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Correct 4 ms 5160 KB Output is correct
12 Correct 3 ms 5160 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 5 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 5 ms 5204 KB Output is correct
17 Correct 5 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5332 KB Output is correct
2 Correct 5 ms 5420 KB Output is correct
3 Partially correct 4 ms 5460 KB Partially correct
4 Partially correct 4 ms 5680 KB Partially correct
5 Partially correct 5 ms 5676 KB Partially correct
6 Correct 4 ms 5588 KB Output is correct
7 Correct 4 ms 5844 KB Output is correct
8 Correct 6 ms 5588 KB Output is correct
9 Partially correct 6 ms 5588 KB Partially correct
10 Correct 5 ms 5716 KB Output is correct
11 Correct 5 ms 5716 KB Output is correct
12 Correct 5 ms 5716 KB Output is correct
13 Correct 4 ms 5588 KB Output is correct
14 Correct 6 ms 5676 KB Output is correct
15 Correct 5 ms 5552 KB Output is correct
16 Correct 4 ms 5680 KB Output is correct
17 Correct 5 ms 5676 KB Output is correct
18 Correct 4 ms 5588 KB Output is correct
19 Correct 4 ms 5588 KB Output is correct
20 Correct 5 ms 5680 KB Output is correct
21 Correct 5 ms 5588 KB Output is correct
22 Correct 6 ms 5588 KB Output is correct
23 Correct 5 ms 5588 KB Output is correct
24 Partially correct 5 ms 5552 KB Partially correct
25 Correct 4 ms 5460 KB Output is correct
26 Partially correct 5 ms 5680 KB Partially correct
27 Correct 4 ms 5416 KB Output is correct
28 Correct 5 ms 5680 KB Output is correct
29 Correct 4 ms 5676 KB Output is correct
30 Correct 5 ms 5588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5204 KB Output is correct
2 Correct 3 ms 5204 KB Output is correct
3 Correct 4 ms 5204 KB Output is correct
4 Correct 4 ms 5204 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 3 ms 5204 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 3 ms 5204 KB Output is correct
9 Correct 3 ms 5164 KB Output is correct
10 Correct 3 ms 5204 KB Output is correct
11 Correct 4 ms 5160 KB Output is correct
12 Correct 3 ms 5160 KB Output is correct
13 Correct 3 ms 5204 KB Output is correct
14 Correct 5 ms 5204 KB Output is correct
15 Correct 3 ms 5204 KB Output is correct
16 Correct 5 ms 5204 KB Output is correct
17 Correct 5 ms 5204 KB Output is correct
18 Correct 3 ms 5332 KB Output is correct
19 Correct 5 ms 5420 KB Output is correct
20 Partially correct 4 ms 5460 KB Partially correct
21 Partially correct 4 ms 5680 KB Partially correct
22 Partially correct 5 ms 5676 KB Partially correct
23 Correct 4 ms 5588 KB Output is correct
24 Correct 4 ms 5844 KB Output is correct
25 Correct 6 ms 5588 KB Output is correct
26 Partially correct 6 ms 5588 KB Partially correct
27 Correct 5 ms 5716 KB Output is correct
28 Correct 5 ms 5716 KB Output is correct
29 Correct 5 ms 5716 KB Output is correct
30 Correct 4 ms 5588 KB Output is correct
31 Correct 6 ms 5676 KB Output is correct
32 Correct 5 ms 5552 KB Output is correct
33 Correct 4 ms 5680 KB Output is correct
34 Correct 5 ms 5676 KB Output is correct
35 Correct 4 ms 5588 KB Output is correct
36 Correct 4 ms 5588 KB Output is correct
37 Correct 5 ms 5680 KB Output is correct
38 Correct 5 ms 5588 KB Output is correct
39 Correct 6 ms 5588 KB Output is correct
40 Correct 5 ms 5588 KB Output is correct
41 Partially correct 5 ms 5552 KB Partially correct
42 Correct 4 ms 5460 KB Output is correct
43 Partially correct 5 ms 5680 KB Partially correct
44 Correct 4 ms 5416 KB Output is correct
45 Correct 5 ms 5680 KB Output is correct
46 Correct 4 ms 5676 KB Output is correct
47 Correct 5 ms 5588 KB Output is correct
48 Partially correct 208 ms 45752 KB Partially correct
49 Partially correct 237 ms 49728 KB Partially correct
50 Partially correct 224 ms 49788 KB Partially correct
51 Partially correct 171 ms 39988 KB Partially correct
52 Partially correct 253 ms 49236 KB Partially correct
53 Partially correct 184 ms 44860 KB Partially correct
54 Correct 121 ms 40620 KB Output is correct
55 Correct 243 ms 77768 KB Output is correct
56 Correct 271 ms 63308 KB Output is correct
57 Correct 247 ms 58472 KB Output is correct
58 Correct 225 ms 54608 KB Output is correct
59 Partially correct 221 ms 50140 KB Partially correct
60 Correct 240 ms 50596 KB Output is correct
61 Correct 201 ms 50340 KB Output is correct
62 Correct 219 ms 50420 KB Output is correct
63 Correct 163 ms 47564 KB Output is correct
64 Partially correct 186 ms 50392 KB Partially correct
65 Correct 192 ms 50772 KB Output is correct
66 Correct 174 ms 47952 KB Output is correct
67 Correct 141 ms 38848 KB Output is correct
68 Correct 171 ms 43900 KB Output is correct
69 Correct 200 ms 50772 KB Output is correct
70 Correct 175 ms 47920 KB Output is correct
71 Correct 120 ms 37064 KB Output is correct
72 Correct 135 ms 41164 KB Output is correct
73 Correct 215 ms 50580 KB Output is correct
74 Correct 163 ms 46716 KB Output is correct
75 Partially correct 216 ms 50584 KB Partially correct
76 Partially correct 211 ms 50000 KB Partially correct
77 Correct 181 ms 48056 KB Output is correct
78 Correct 111 ms 35532 KB Output is correct
79 Correct 162 ms 40340 KB Output is correct
80 Partially correct 188 ms 50500 KB Partially correct
81 Correct 183 ms 48196 KB Output is correct
82 Correct 199 ms 49792 KB Output is correct