Submission #414037

# Submission time Handle Problem Language Result Execution time Memory
414037 2021-05-29T19:29:11 Z ritul_kr_singh Village (BOI20_village) C++17
100 / 100
123 ms 19772 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'

const int MAXN = 1e5;

int n, s[MAXN], c, ans[2][MAXN], ansMax = 0, ansMin = 0;
vector<int> g[MAXN], a[MAXN];

void find(int u){
	int mx = 0; s[u] = 1;
	for(int v : g[u]){
		if(s[v]) continue;
		find(v), s[u] += s[v];
		mx = max(mx, s[v]);
		ansMax += min(n - s[v], s[v]);
		if(ans[0][v] == v) swap(ans[0][u], ans[0][v]), ansMin += 2;
	}
	if(max(mx, n - s[u]) <= n/2) c = u;
}

void add(int u, int p, vector<int> &b){
	b.push_back(u);
	for(int v : g[u]) if(v != p) add(v, u, b);
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int x, y; cin >> n;
	for(int i=1; i<n; ++i){
		cin >> x >> y; --x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	for(int i=0; i<2; ++i) iota(ans[i], ans[i]+n, 0LL);

	find(0);

	priority_queue<array<int, 2>> q;
	if(n & 1) swap(ans[1][c], ans[1][!c]);
	else a[c].push_back(c), q.push({1, c});
	for(int u : g[c]){
		add(u, c, a[u]);
		q.push({(int)a[u].size(), u});
	}

	while(!q.empty()){
		assert(q.size() > 1);
		array<int, 2> u = q.top(); q.pop();
		array<int, 2> v = q.top(); q.pop();
		swap(ans[1][a[u[1]].back()], ans[1][a[v[1]].back()]);
		a[u[1]].pop_back(), a[v[1]].pop_back();
		if(--u[0]) q.push(u);
		if(--v[0]) q.push(v);
	}

	for(int i=0; i<n; ++i) if(ans[0][i] == i) swap(ans[0][i], ans[0][g[i][0]]), ansMin += 2;

	cout << ansMin sp 2 * ansMax nl;
	for(int i=0; i<2; ++i){
		for(int j=0; j<n; ++j) cout << ans[i][j] + 1 << ' ';
		cout nl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 5020 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 4 ms 5020 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 4 ms 4924 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 5068 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5068 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5068 KB Output is correct
7 Correct 4 ms 5024 KB Output is correct
8 Correct 4 ms 5036 KB Output is correct
9 Correct 4 ms 5068 KB Output is correct
10 Correct 4 ms 5068 KB Output is correct
11 Correct 4 ms 5068 KB Output is correct
12 Correct 4 ms 5068 KB Output is correct
13 Correct 5 ms 5068 KB Output is correct
14 Correct 5 ms 5068 KB Output is correct
15 Correct 5 ms 5068 KB Output is correct
16 Correct 4 ms 5068 KB Output is correct
17 Correct 4 ms 5068 KB Output is correct
18 Correct 4 ms 5068 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 5 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 5 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 5020 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 4 ms 4940 KB Output is correct
7 Correct 4 ms 5020 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 4 ms 5020 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 4 ms 4924 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 3 ms 5020 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5024 KB Output is correct
25 Correct 4 ms 5036 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 5 ms 5068 KB Output is correct
31 Correct 5 ms 5068 KB Output is correct
32 Correct 5 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5068 KB Output is correct
39 Correct 4 ms 5068 KB Output is correct
40 Correct 5 ms 5068 KB Output is correct
41 Correct 4 ms 5068 KB Output is correct
42 Correct 4 ms 5068 KB Output is correct
43 Correct 4 ms 5068 KB Output is correct
44 Correct 5 ms 5068 KB Output is correct
45 Correct 4 ms 5068 KB Output is correct
46 Correct 4 ms 5068 KB Output is correct
47 Correct 4 ms 5068 KB Output is correct
48 Correct 75 ms 13524 KB Output is correct
49 Correct 93 ms 14456 KB Output is correct
50 Correct 99 ms 14456 KB Output is correct
51 Correct 61 ms 12360 KB Output is correct
52 Correct 80 ms 14440 KB Output is correct
53 Correct 73 ms 13416 KB Output is correct
54 Correct 40 ms 12224 KB Output is correct
55 Correct 101 ms 19772 KB Output is correct
56 Correct 90 ms 17080 KB Output is correct
57 Correct 98 ms 16084 KB Output is correct
58 Correct 87 ms 15160 KB Output is correct
59 Correct 91 ms 14496 KB Output is correct
60 Correct 123 ms 18196 KB Output is correct
61 Correct 79 ms 15372 KB Output is correct
62 Correct 76 ms 14660 KB Output is correct
63 Correct 81 ms 14100 KB Output is correct
64 Correct 78 ms 14784 KB Output is correct
65 Correct 97 ms 14920 KB Output is correct
66 Correct 82 ms 14368 KB Output is correct
67 Correct 62 ms 12664 KB Output is correct
68 Correct 66 ms 13384 KB Output is correct
69 Correct 85 ms 15016 KB Output is correct
70 Correct 71 ms 14400 KB Output is correct
71 Correct 52 ms 11864 KB Output is correct
72 Correct 57 ms 12704 KB Output is correct
73 Correct 78 ms 15024 KB Output is correct
74 Correct 67 ms 14196 KB Output is correct
75 Correct 98 ms 14748 KB Output is correct
76 Correct 78 ms 14448 KB Output is correct
77 Correct 76 ms 14528 KB Output is correct
78 Correct 48 ms 11456 KB Output is correct
79 Correct 60 ms 12468 KB Output is correct
80 Correct 92 ms 14880 KB Output is correct
81 Correct 76 ms 14708 KB Output is correct
82 Correct 80 ms 14536 KB Output is correct