Submission #500415

# Submission time Handle Problem Language Result Execution time Memory
500415 2021-12-31T00:55:52 Z zhougz Village (BOI20_village) C++17
100 / 100
110 ms 28032 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

int n;
const int MAXN = 100'050;
vector<vector<int>> v(MAXN, vector<int>());

pair<int, vector<int>> slv1(int n, vector<vector<int>> v) {
	int res = 0;
	vector<int> arr(n); // arr[i] denotes guy at place where i was
	iota(arr.begin(), arr.end(), 0);
	function<void(int, int)> dfs = [&](int x, int p) -> void {
		for (auto z : v[x]) {
			if (z != p) {
				dfs(z, x);
			}
		}
		if (arr[x] == x) {
			if (x != 0) {
				swap(arr[x], arr[p]);
			} else {
				swap(arr[0], arr[v[0][0]]);
			}
			res += 2;
		}
	};
	dfs(0, -1);
	vector<int> ans(n);
	for (int i = 0; i < n; i++) {
		ans[arr[i]] = i;
	}
	return make_pair(res, ans);
}

pair<int, vector<int>> slv2(int n, vector<vector<int>> v) {
	int mx = 0, cnt = 0, centroid = -1;
	vector<int> sz(n), pre(n);
	function<void(int, int)> dfs = [&](int x, int p) -> void {
		pre[cnt++] = x;
		sz[x] = 1;
		for (auto z : v[x]) {
			if (z != p) {
				dfs(z, x);
				sz[x] += sz[z];
				mx += min(sz[z], n - sz[z]);
			}
		}
		if (sz[x] > n / 2 && centroid == -1) {
			centroid = x;
		}
	};
	dfs(0, -1);
	vector<int> ans(n);
	for (int i = 0; i < n; i++) {
		ans[pre[i]] = pre[(i + n / 2) % n];
	}
	return make_pair(2 * mx, ans);
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 0, a, b; i < n - 1; i++) {
		cin >> a >> b;
		a--;
		b--;
		v[a].push_back(b);
		v[b].push_back(a);
	}
	int res1, res2;
	vector<int> arr1, arr2;
	tie(res1, arr1) = slv1(n, v);
	tie(res2, arr2) = slv2(n, v);
	cout << res1 << ' ' << res2 << '\n';
	for (auto x : arr1) {
		cout << x + 1 << ' ';
	}
	cout << '\n';
	for (auto x : arr2) {
		cout << x + 1 << ' ';
	}
	cout << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5024 KB Output is correct
2 Correct 4 ms 4932 KB Output is correct
3 Correct 4 ms 4964 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 6 ms 4980 KB Output is correct
6 Correct 4 ms 5024 KB Output is correct
7 Correct 4 ms 4980 KB Output is correct
8 Correct 4 ms 4984 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4976 KB Output is correct
11 Correct 4 ms 5024 KB Output is correct
12 Correct 5 ms 4972 KB Output is correct
13 Correct 4 ms 5024 KB Output is correct
14 Correct 4 ms 5024 KB Output is correct
15 Correct 4 ms 5024 KB Output is correct
16 Correct 4 ms 4972 KB Output is correct
17 Correct 4 ms 4976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5024 KB Output is correct
2 Correct 6 ms 5056 KB Output is correct
3 Correct 4 ms 5068 KB Output is correct
4 Correct 4 ms 5152 KB Output is correct
5 Correct 4 ms 5152 KB Output is correct
6 Correct 5 ms 5152 KB Output is correct
7 Correct 4 ms 5200 KB Output is correct
8 Correct 5 ms 5152 KB Output is correct
9 Correct 4 ms 5116 KB Output is correct
10 Correct 5 ms 5152 KB Output is correct
11 Correct 7 ms 5152 KB Output is correct
12 Correct 5 ms 5144 KB Output is correct
13 Correct 6 ms 5152 KB Output is correct
14 Correct 5 ms 5152 KB Output is correct
15 Correct 5 ms 5152 KB Output is correct
16 Correct 4 ms 5152 KB Output is correct
17 Correct 5 ms 5120 KB Output is correct
18 Correct 5 ms 5124 KB Output is correct
19 Correct 4 ms 5152 KB Output is correct
20 Correct 5 ms 5152 KB Output is correct
21 Correct 5 ms 5124 KB Output is correct
22 Correct 5 ms 5116 KB Output is correct
23 Correct 5 ms 5068 KB Output is correct
24 Correct 5 ms 5068 KB Output is correct
25 Correct 5 ms 5064 KB Output is correct
26 Correct 5 ms 5152 KB Output is correct
27 Correct 4 ms 5064 KB Output is correct
28 Correct 4 ms 5152 KB Output is correct
29 Correct 5 ms 5152 KB Output is correct
30 Correct 5 ms 5112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5024 KB Output is correct
2 Correct 4 ms 4932 KB Output is correct
3 Correct 4 ms 4964 KB Output is correct
4 Correct 5 ms 4940 KB Output is correct
5 Correct 6 ms 4980 KB Output is correct
6 Correct 4 ms 5024 KB Output is correct
7 Correct 4 ms 4980 KB Output is correct
8 Correct 4 ms 4984 KB Output is correct
9 Correct 4 ms 4972 KB Output is correct
10 Correct 4 ms 4976 KB Output is correct
11 Correct 4 ms 5024 KB Output is correct
12 Correct 5 ms 4972 KB Output is correct
13 Correct 4 ms 5024 KB Output is correct
14 Correct 4 ms 5024 KB Output is correct
15 Correct 4 ms 5024 KB Output is correct
16 Correct 4 ms 4972 KB Output is correct
17 Correct 4 ms 4976 KB Output is correct
18 Correct 7 ms 5024 KB Output is correct
19 Correct 6 ms 5056 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 4 ms 5152 KB Output is correct
22 Correct 4 ms 5152 KB Output is correct
23 Correct 5 ms 5152 KB Output is correct
24 Correct 4 ms 5200 KB Output is correct
25 Correct 5 ms 5152 KB Output is correct
26 Correct 4 ms 5116 KB Output is correct
27 Correct 5 ms 5152 KB Output is correct
28 Correct 7 ms 5152 KB Output is correct
29 Correct 5 ms 5144 KB Output is correct
30 Correct 6 ms 5152 KB Output is correct
31 Correct 5 ms 5152 KB Output is correct
32 Correct 5 ms 5152 KB Output is correct
33 Correct 4 ms 5152 KB Output is correct
34 Correct 5 ms 5120 KB Output is correct
35 Correct 5 ms 5124 KB Output is correct
36 Correct 4 ms 5152 KB Output is correct
37 Correct 5 ms 5152 KB Output is correct
38 Correct 5 ms 5124 KB Output is correct
39 Correct 5 ms 5116 KB Output is correct
40 Correct 5 ms 5068 KB Output is correct
41 Correct 5 ms 5068 KB Output is correct
42 Correct 5 ms 5064 KB Output is correct
43 Correct 5 ms 5152 KB Output is correct
44 Correct 4 ms 5064 KB Output is correct
45 Correct 4 ms 5152 KB Output is correct
46 Correct 5 ms 5152 KB Output is correct
47 Correct 5 ms 5112 KB Output is correct
48 Correct 97 ms 17212 KB Output is correct
49 Correct 90 ms 18392 KB Output is correct
50 Correct 95 ms 18460 KB Output is correct
51 Correct 63 ms 15480 KB Output is correct
52 Correct 92 ms 18228 KB Output is correct
53 Correct 87 ms 16972 KB Output is correct
54 Correct 42 ms 16064 KB Output is correct
55 Correct 110 ms 28032 KB Output is correct
56 Correct 91 ms 22904 KB Output is correct
57 Correct 95 ms 21200 KB Output is correct
58 Correct 91 ms 19824 KB Output is correct
59 Correct 86 ms 18548 KB Output is correct
60 Correct 82 ms 18940 KB Output is correct
61 Correct 73 ms 19112 KB Output is correct
62 Correct 86 ms 19108 KB Output is correct
63 Correct 68 ms 18260 KB Output is correct
64 Correct 105 ms 19148 KB Output is correct
65 Correct 69 ms 19368 KB Output is correct
66 Correct 90 ms 18380 KB Output is correct
67 Correct 49 ms 15340 KB Output is correct
68 Correct 82 ms 17164 KB Output is correct
69 Correct 76 ms 19448 KB Output is correct
70 Correct 65 ms 18456 KB Output is correct
71 Correct 48 ms 14928 KB Output is correct
72 Correct 84 ms 16356 KB Output is correct
73 Correct 71 ms 19444 KB Output is correct
74 Correct 84 ms 18200 KB Output is correct
75 Correct 87 ms 18292 KB Output is correct
76 Correct 98 ms 18516 KB Output is correct
77 Correct 79 ms 18652 KB Output is correct
78 Correct 50 ms 14292 KB Output is correct
79 Correct 55 ms 15756 KB Output is correct
80 Correct 104 ms 18428 KB Output is correct
81 Correct 71 ms 18816 KB Output is correct
82 Correct 90 ms 18988 KB Output is correct