답안 #962837

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
962837 2024-04-14T08:51:10 Z josanneo22 Village (BOI20_village) C++17
75 / 100
77 ms 20408 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 (sz[v] > N / 2) 
			return find_centroid(v, u);
	}
	return u;
}
pair<int, 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 2 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 7000 KB Output is correct
4 Correct 1 ms 6796 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 1 ms 6556 KB Output is correct
11 Correct 1 ms 6660 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6744 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6768 KB Output is correct
6 Correct 2 ms 6748 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6800 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 2 ms 6748 KB Output is correct
11 Correct 2 ms 6748 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 6796 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 3 ms 6748 KB Output is correct
21 Correct 2 ms 6748 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 2 ms 6748 KB Output is correct
24 Correct 2 ms 6748 KB Output is correct
25 Correct 2 ms 6748 KB Output is correct
26 Correct 2 ms 6800 KB Output is correct
27 Correct 2 ms 6748 KB Output is correct
28 Correct 2 ms 6748 KB Output is correct
29 Correct 2 ms 6748 KB Output is correct
30 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 1 ms 6748 KB Output is correct
3 Correct 1 ms 7000 KB Output is correct
4 Correct 1 ms 6796 KB Output is correct
5 Correct 1 ms 6748 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6744 KB Output is correct
8 Correct 1 ms 6748 KB Output is correct
9 Correct 2 ms 6748 KB Output is correct
10 Correct 1 ms 6556 KB Output is correct
11 Correct 1 ms 6660 KB Output is correct
12 Correct 1 ms 6748 KB Output is correct
13 Correct 1 ms 6748 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6744 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 2 ms 6748 KB Output is correct
21 Correct 2 ms 6748 KB Output is correct
22 Correct 2 ms 6768 KB Output is correct
23 Correct 2 ms 6748 KB Output is correct
24 Correct 2 ms 6748 KB Output is correct
25 Correct 2 ms 6800 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 6748 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 6796 KB Output is correct
36 Correct 2 ms 6744 KB Output is correct
37 Correct 3 ms 6748 KB Output is correct
38 Correct 2 ms 6748 KB Output is correct
39 Correct 2 ms 6748 KB Output is correct
40 Correct 2 ms 6748 KB Output is correct
41 Correct 2 ms 6748 KB Output is correct
42 Correct 2 ms 6748 KB Output is correct
43 Correct 2 ms 6800 KB Output is correct
44 Correct 2 ms 6748 KB Output is correct
45 Correct 2 ms 6748 KB Output is correct
46 Correct 2 ms 6748 KB Output is correct
47 Correct 2 ms 6748 KB Output is correct
48 Correct 41 ms 13632 KB Output is correct
49 Correct 43 ms 14276 KB Output is correct
50 Correct 46 ms 14348 KB Output is correct
51 Correct 34 ms 12628 KB Output is correct
52 Correct 45 ms 14328 KB Output is correct
53 Correct 39 ms 13652 KB Output is correct
54 Correct 25 ms 13388 KB Output is correct
55 Partially correct 73 ms 20408 KB Partially correct
56 Partially correct 52 ms 17056 KB Partially correct
57 Correct 77 ms 16200 KB Output is correct
58 Correct 54 ms 15192 KB Output is correct
59 Correct 51 ms 14512 KB Output is correct
60 Correct 33 ms 14548 KB Output is correct
61 Correct 40 ms 14816 KB Output is correct
62 Correct 47 ms 14708 KB Output is correct
63 Correct 38 ms 14172 KB Output is correct
64 Correct 42 ms 14680 KB Output is correct
65 Correct 36 ms 14676 KB Output is correct
66 Correct 34 ms 14164 KB Output is correct
67 Correct 26 ms 12616 KB Output is correct
68 Correct 31 ms 13680 KB Output is correct
69 Correct 38 ms 14924 KB Output is correct
70 Correct 34 ms 14412 KB Output is correct
71 Correct 30 ms 12292 KB Output is correct
72 Correct 29 ms 13136 KB Output is correct
73 Correct 36 ms 14920 KB Output is correct
74 Correct 33 ms 14156 KB Output is correct
75 Correct 62 ms 14344 KB Output is correct
76 Correct 43 ms 14156 KB Output is correct
77 Correct 39 ms 14392 KB Output is correct
78 Correct 33 ms 11872 KB Output is correct
79 Correct 30 ms 12620 KB Output is correct
80 Correct 42 ms 14320 KB Output is correct
81 Correct 41 ms 14420 KB Output is correct
82 Correct 42 ms 14548 KB Output is correct