#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
using pii = pair<int, int>;
template<typename T>
using prior = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using Prior = priority_queue<T>;
#define X first
#define Y second
#define ALL(x) (x).begin(), (x).end()
#define eb emplace_back
#define pb push_back
#define fastIO() ios_base::sync_with_stdio(false), cin.tie(0)
const int maxn = 1E5 + 5;
int n, cnt1 = 0, cnt2 = 0;
vector<int> ans1, ans2;
vector<int> adj[maxn], par(maxn), sz(maxn), pl(maxn);
deque<int> deq;
void dfs(int now, int lst) {
par[now] = lst;
for (auto x : adj[now]) {
if (x != lst) dfs(x, now);
}
if (now != 1 and pl[now] == now) ++cnt1, swap(pl[now], pl[par[now]]);
}
void dfs_sz(int now, int lst) {
sz[now] = 1, par[now] = lst;
for (auto x : adj[now]) {
if (x != lst) dfs_sz(x, now), sz[now] += sz[x];
}
}
void dfs_get(int now, int lst) {
deq.push_back(now);
for (auto x : adj[now]) {
if (x != lst) dfs_get(x, now);
}
}
void dfs_put(int now, int lst) {
pl[now] = deq.front(), deq.pop_front();
for (auto x : adj[now]) {
if (x != lst) dfs_put(x, now);
}
}
void solve1() {
iota(ALL(pl), 0);
dfs(1, -1);
if (pl[1] == 1) ++cnt1, swap(pl[1], pl[adj[1][0]]);
cnt1 *= 2, ans1 = vector<int>(pl.begin()+1, pl.begin()+n+1);
}
void solve2() {
iota(ALL(pl), 0);
dfs_sz(1, -1);
int rt = 0;
for (int i = 1; i <= n; ++i) {
int maxCh = 0;
for (auto x : adj[i]) {
if (x != par[i]) maxCh = max(maxCh, sz[x]);
else maxCh = max(maxCh, n - sz[i]);
}
if (maxCh <= n/2) {rt = i; break;}
}
dfs_sz(rt, -1);
sort(ALL(adj[rt]), [](auto a, auto b) {return sz[a] > sz[b];});
int rt_ch = adj[rt].size();
for (int i = 0; i < rt_ch; ++i) dfs_get(adj[rt][i], rt);
for (int i = 1; i < rt_ch; ++i) dfs_put(adj[rt][i], rt);
pl[rt] = deq.front(), deq.pop_front();
deq.push_back(rt), dfs_put(adj[rt][0], rt);
for (int i = 1; i <= n; ++i) cnt2 += min(sz[i], n - sz[i]);
cnt2 *= 2, ans2 = vector<int>(pl.begin()+1, pl.begin()+n+1);
}
int32_t main() {
fastIO();
cin >> n;
for (int i = 0; i < n-1; ++i) {
int u, v; cin >> u >> v;
adj[u].eb(v), adj[v].eb(u);
}
solve1(), solve2();
cout << cnt1 << " " << cnt2 << "\n";
for (auto x : ans1) cout << x << " "; cout << "\n";
for (auto x : ans2) cout << x << " "; cout << "\n";
return 0;
}
Compilation message
Village.cpp: In function 'int32_t main()':
Village.cpp:109:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
109 | for (auto x : ans1) cout << x << " "; cout << "\n";
| ^~~
Village.cpp:109:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
109 | for (auto x : ans1) cout << x << " "; cout << "\n";
| ^~~~
Village.cpp:110:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
110 | for (auto x : ans2) cout << x << " "; cout << "\n";
| ^~~
Village.cpp:110:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
110 | for (auto x : ans2) cout << x << " "; cout << "\n";
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
5032 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5024 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5032 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5040 KB |
Output is correct |
11 |
Correct |
3 ms |
5068 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
3 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5036 KB |
Output is correct |
18 |
Correct |
3 ms |
5040 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
2 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
2 ms |
4940 KB |
Output is correct |
7 |
Correct |
2 ms |
5032 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
2 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
2 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
2 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
5068 KB |
Output is correct |
20 |
Correct |
3 ms |
5032 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5040 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5036 KB |
Output is correct |
35 |
Correct |
3 ms |
5040 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
3 ms |
5068 KB |
Output is correct |
38 |
Correct |
3 ms |
5068 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
3 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
3 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5068 KB |
Output is correct |
47 |
Correct |
3 ms |
5068 KB |
Output is correct |
48 |
Correct |
65 ms |
12672 KB |
Output is correct |
49 |
Correct |
74 ms |
13504 KB |
Output is correct |
50 |
Correct |
74 ms |
13536 KB |
Output is correct |
51 |
Correct |
54 ms |
11588 KB |
Output is correct |
52 |
Correct |
74 ms |
13316 KB |
Output is correct |
53 |
Correct |
62 ms |
12536 KB |
Output is correct |
54 |
Correct |
37 ms |
11716 KB |
Output is correct |
55 |
Correct |
101 ms |
18836 KB |
Output is correct |
56 |
Correct |
100 ms |
16160 KB |
Output is correct |
57 |
Correct |
88 ms |
15116 KB |
Output is correct |
58 |
Correct |
94 ms |
14300 KB |
Output is correct |
59 |
Correct |
81 ms |
13552 KB |
Output is correct |
60 |
Correct |
54 ms |
13580 KB |
Output is correct |
61 |
Correct |
58 ms |
13620 KB |
Output is correct |
62 |
Correct |
59 ms |
13712 KB |
Output is correct |
63 |
Correct |
56 ms |
13252 KB |
Output is correct |
64 |
Correct |
63 ms |
13884 KB |
Output is correct |
65 |
Correct |
59 ms |
14008 KB |
Output is correct |
66 |
Correct |
56 ms |
13236 KB |
Output is correct |
67 |
Correct |
40 ms |
11328 KB |
Output is correct |
68 |
Correct |
49 ms |
12516 KB |
Output is correct |
69 |
Correct |
60 ms |
13960 KB |
Output is correct |
70 |
Correct |
61 ms |
13400 KB |
Output is correct |
71 |
Correct |
39 ms |
11204 KB |
Output is correct |
72 |
Correct |
43 ms |
12084 KB |
Output is correct |
73 |
Correct |
62 ms |
14020 KB |
Output is correct |
74 |
Correct |
51 ms |
13124 KB |
Output is correct |
75 |
Correct |
79 ms |
13888 KB |
Output is correct |
76 |
Correct |
79 ms |
13380 KB |
Output is correct |
77 |
Correct |
66 ms |
13740 KB |
Output is correct |
78 |
Correct |
39 ms |
10820 KB |
Output is correct |
79 |
Correct |
49 ms |
11712 KB |
Output is correct |
80 |
Correct |
77 ms |
13932 KB |
Output is correct |
81 |
Correct |
61 ms |
13868 KB |
Output is correct |
82 |
Correct |
63 ms |
13636 KB |
Output is correct |