답안 #916517

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
916517 2024-01-26T03:32:40 Z NK_ Village (BOI20_village) C++17
100 / 100
67 ms 19312 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())

using ll = long long;
template<class T> using V = vector<T>;
using vi = V<int>;


int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; cin >> N;

	V<vi> adj(N); for(int i = 0; i < N - 1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}

	vi sub(N); function<void(int, int)> gen = [&](int u, int p) {
		sub[u] = 1;
		for(auto& v : adj[u]) if (v != p) {
			gen(v, u); sub[u] += sub[v];
		}
	};

	function<int(int, int)> find = [&](int u, int p) {
		for(auto& v : adj[u]) if (v != p) {
			if (2 * sub[v] >= N) return find(v, u);
		}

		return u;
	};

	gen(0, -1); int R = find(0, -1);

	// cout << R << endl;

	ll lans = 0, gans = 0;
	vi L(N), G(N); iota(begin(L), end(L), 0);

	vi ord;
	function<int(int, int)> answer = [&](int u, int p) {
		ord.pb(u); sub[u] = 1;

		vi chd = {u};
		for(auto& v : adj[u]) if (v != p) {
			int r = answer(v, u); 
			sub[u] += sub[v];
			gans += 2 * sub[v];
			if (r != -1) chd.pb(r);
		}


		if (sz(chd) == 1) {
			lans += 2;
			return chd[0];
		}

		for(int i = 0; i < sz(chd); i++) {
			int j = (i + 1) % sz(chd);
			L[chd[j]] = chd[i];
		}

		return -1;
	};

	int r = answer(R, -1);
	if (r != -1) {
		for(auto& v : adj[R]) {
			swap(L[R], L[v]);
			break;
		}
	}

	int mx = 0;
	for(auto& v : adj[R]) mx = max(mx, sub[v]);

	for(int x = 0; x < N; x++) {
		int y = (x + mx) % N;
		G[ord[y]] = ord[x];
	}

	cout << lans << " " << gans << nl;
	
	for(auto& x : L) cout << x + 1 << " ";
	cout << nl;

	for(auto& x : G) cout << x + 1 << " ";
	cout << nl;





	exit(0-0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 408 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 600 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 408 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 44 ms 8192 KB Output is correct
49 Correct 52 ms 8908 KB Output is correct
50 Correct 47 ms 8912 KB Output is correct
51 Correct 37 ms 6860 KB Output is correct
52 Correct 45 ms 8652 KB Output is correct
53 Correct 41 ms 7892 KB Output is correct
54 Correct 27 ms 9484 KB Output is correct
55 Correct 67 ms 19312 KB Output is correct
56 Correct 52 ms 13692 KB Output is correct
57 Correct 56 ms 11980 KB Output is correct
58 Correct 48 ms 10700 KB Output is correct
59 Correct 47 ms 8908 KB Output is correct
60 Correct 35 ms 9408 KB Output is correct
61 Correct 36 ms 9264 KB Output is correct
62 Correct 37 ms 9192 KB Output is correct
63 Correct 35 ms 8664 KB Output is correct
64 Correct 42 ms 9172 KB Output is correct
65 Correct 39 ms 9176 KB Output is correct
66 Correct 42 ms 8640 KB Output is correct
67 Correct 26 ms 7124 KB Output is correct
68 Correct 33 ms 7880 KB Output is correct
69 Correct 36 ms 9352 KB Output is correct
70 Correct 40 ms 8648 KB Output is correct
71 Correct 26 ms 6600 KB Output is correct
72 Correct 30 ms 7376 KB Output is correct
73 Correct 52 ms 9416 KB Output is correct
74 Correct 34 ms 8396 KB Output is correct
75 Correct 42 ms 8648 KB Output is correct
76 Correct 44 ms 8652 KB Output is correct
77 Correct 42 ms 8736 KB Output is correct
78 Correct 27 ms 6076 KB Output is correct
79 Correct 31 ms 7168 KB Output is correct
80 Correct 44 ms 8644 KB Output is correct
81 Correct 36 ms 8908 KB Output is correct
82 Correct 38 ms 8920 KB Output is correct