답안 #962838

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
962838 2024-04-14T08:52:22 Z josanneo22 Village (BOI20_village) C++17
100 / 100
54 ms 19096 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

const int nax = 200005;
int rep[nax], par[nax], N, sz[nax];
bool swp[nax];
vector<int> G[nax], tab, pos;

void dfs(int u, int f) {
	par[u] = f;
	tab.push_back(u);
	for (auto v : G[u]) {
		if (v == f) continue;
		dfs(v, u);
	}
}
pair<int, vector<int>> smallest() {
	for (int i = 1; i <= N; i++) {
		rep[i] = i;
		swp[i] = false;
		par[i] = -1;
	}
	dfs(1, -1);
	reverse(tab.begin(), tab.end());
	int ans = 0;
	for (auto u : tab) {
		if (swp[u] == false) {
			int v = par[u];
			if (v == -1) v = G[u][0];
			swp[u] = swp[v] = true;
			swap(rep[u], rep[v]);
			ans += 2;
		}
	}						 
	tab.clear();
	for (int i = 1; i <= N; i++) 
		pos.push_back(rep[i]);
	return { ans, pos };
}
i64 sum = 0;
void dfs_size(int u, int f) {
	tab.push_back(u);
	sz[u] = 1;
	for (auto v : G[u]) {
		if (v == f) continue;
		dfs_size(v, u);
		sz[u] += sz[v];
	}
	sum += 2LL * min(sz[u], (N - sz[u]));
}
int find_centroid(int u, int f) {
	for (auto v : G[u]) {
		if (v == f) continue;
		if (2 * sz[v] > N) 
			return find_centroid(v, u);
	}
	return u;
}
pair<i64, vector<int>> largest() {
	dfs_size(1, -1);
	int root = find_centroid(1, -1);
	tab.clear(); pos.clear();
	sum = 0;
	dfs_size(root, -1);
	for (int i = 0; i < N; i++) 
		rep[tab[i]] = tab[(i + N / 2) % N];
	for (int i = 1; i <= N; i++)
		pos.push_back(rep[i]);
	return { sum, pos };
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	cin >> N;
	for (int i = 1; i <= N - 1; i++) {
		int u, v; cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	auto A = smallest();
	auto B = largest();
	cout << A.first << ' ' << B.first << '\n';
	for (auto o : A.second) cout << o << " \n"[o == A.second.back()];
	for (auto o : B.second) cout << o << " \n"[o == B.second.back()];
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 7000 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 1 ms 6744 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 1 ms 6744 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 1 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 2 ms 6744 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 3 ms 6744 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 3 ms 6748 KB Output is correct
7 Correct 2 ms 7000 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 2 ms 6744 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 2 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 2 ms 6748 KB Output is correct
20 Correct 3 ms 6748 KB Output is correct
21 Correct 2 ms 6744 KB Output is correct
22 Correct 2 ms 6744 KB Output is correct
23 Correct 3 ms 6748 KB Output is correct
24 Correct 2 ms 6748 KB Output is correct
25 Correct 2 ms 6772 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 2 ms 6748 KB Output is correct
28 Correct 2 ms 6756 KB Output is correct
29 Correct 2 ms 6744 KB Output is correct
30 Correct 2 ms 6744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 6748 KB Output is correct
4 Correct 1 ms 7000 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 1 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 1 ms 6744 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 1 ms 6748 KB Output is correct
15 Correct 1 ms 6744 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 1 ms 6748 KB Output is correct
18 Correct 2 ms 6748 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 1 ms 6748 KB Output is correct
21 Correct 3 ms 6744 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 3 ms 6748 KB Output is correct
24 Correct 2 ms 7000 KB Output is correct
25 Correct 2 ms 6748 KB Output is correct
26 Correct 2 ms 6748 KB Output is correct
27 Correct 2 ms 6744 KB Output is correct
28 Correct 2 ms 6744 KB Output is correct
29 Correct 2 ms 6748 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
31 Correct 2 ms 6748 KB Output is correct
32 Correct 2 ms 6748 KB Output is correct
33 Correct 2 ms 6748 KB Output is correct
34 Correct 2 ms 6748 KB Output is correct
35 Correct 2 ms 6748 KB Output is correct
36 Correct 2 ms 6748 KB Output is correct
37 Correct 3 ms 6748 KB Output is correct
38 Correct 2 ms 6744 KB Output is correct
39 Correct 2 ms 6744 KB Output is correct
40 Correct 3 ms 6748 KB Output is correct
41 Correct 2 ms 6748 KB Output is correct
42 Correct 2 ms 6772 KB Output is correct
43 Correct 2 ms 6748 KB Output is correct
44 Correct 2 ms 6748 KB Output is correct
45 Correct 2 ms 6756 KB Output is correct
46 Correct 2 ms 6744 KB Output is correct
47 Correct 2 ms 6744 KB Output is correct
48 Correct 40 ms 12720 KB Output is correct
49 Correct 43 ms 13140 KB Output is correct
50 Correct 48 ms 13224 KB Output is correct
51 Correct 34 ms 11664 KB Output is correct
52 Correct 42 ms 13128 KB Output is correct
53 Correct 39 ms 12632 KB Output is correct
54 Correct 33 ms 12780 KB Output is correct
55 Correct 54 ms 19096 KB Output is correct
56 Correct 49 ms 15952 KB Output is correct
57 Correct 48 ms 14932 KB Output is correct
58 Correct 47 ms 14000 KB Output is correct
59 Correct 44 ms 13168 KB Output is correct
60 Correct 35 ms 13592 KB Output is correct
61 Correct 36 ms 13628 KB Output is correct
62 Correct 40 ms 13644 KB Output is correct
63 Correct 39 ms 13148 KB Output is correct
64 Correct 41 ms 13652 KB Output is correct
65 Correct 35 ms 13656 KB Output is correct
66 Correct 37 ms 13152 KB Output is correct
67 Correct 26 ms 11848 KB Output is correct
68 Correct 31 ms 12628 KB Output is correct
69 Correct 36 ms 13644 KB Output is correct
70 Correct 35 ms 13144 KB Output is correct
71 Correct 24 ms 11560 KB Output is correct
72 Correct 28 ms 12104 KB Output is correct
73 Correct 36 ms 13652 KB Output is correct
74 Correct 34 ms 13132 KB Output is correct
75 Correct 43 ms 13144 KB Output is correct
76 Correct 42 ms 13032 KB Output is correct
77 Correct 38 ms 13388 KB Output is correct
78 Correct 27 ms 11104 KB Output is correct
79 Correct 32 ms 11788 KB Output is correct
80 Correct 47 ms 13136 KB Output is correct
81 Correct 38 ms 13372 KB Output is correct
82 Correct 37 ms 13388 KB Output is correct