#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100'000;
int n;
int resMin[N + 10], ansMin;
int resMax[N + 10], root, cnt, sz[N + 10];
vector<int> base[N + 10], vec[N + 10], res[N + 10];
vector<int> adj[N + 10];
ll ansMax;
void readInput() {
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
}
void calcSz(int u = 1, int par = 0) {
sz[u] = 1;
for (auto v: adj[u])
if (v != par) {
calcSz(v, u);
sz[u] += sz[v];
}
ansMax += (ll) min(sz[u], n - sz[u]) * 2ll;
}
int getCentroid() {
int res = 1;
while (true) {
int ok = res;
for (auto v: adj[res])
if (sz[v] < sz[res] && sz[v] > n / 2)
ok = v;
if (ok == res)
return res;
res = ok;
}
}
void addBase(int u, int par) {
base[cnt].push_back(u);
for (auto v: adj[u])
if (v != par)
addBase(v, u);
}
void calcBase(int root) {
base[++cnt].push_back(root);
vec[cnt] = base[cnt];
for (auto u: adj[root]) {
cnt++;
addBase(u, root);
vec[cnt] = base[cnt];
}
}
void calcResVectors() {
set<pair<int, int>> st;
for (int i = 1; i <= cnt; i++)
st.insert({base[i].size(), i});
while (st.size() >= 2) {
int idx1 = st.rbegin() -> second;
st.erase(*st.rbegin());
int idx2 = st.rbegin() -> second;
st.erase(*st.rbegin());
int ver1 = vec[idx1].back(), ver2 = vec[idx2].back();
res[idx1].push_back(ver2);
res[idx2].push_back(ver1);
vec[idx1].pop_back();
vec[idx2].pop_back();
if (vec[idx1].size())
st.insert({vec[idx1].size(), idx1});
if (vec[idx2].size())
st.insert({vec[idx2].size(), idx2});
}
if (st.size()) {
res[1].push_back(base[1][0]);
swap(res[2].back(), res[1].back());
}
}
void calcResMax() {
for (int i = 1; i <= cnt; i++)
for (int j = 0; j < base[i].size(); j++)
resMax[base[i][j]] = res[i][j];
}
void solveMax() {
calcSz();
root = getCentroid();
calcBase(root);
calcResVectors();
calcResMax();
}
int dfs(int u = 1, int par = 0) {
vector<int> vec;
for (auto v: adj[u])
if (v != par) {
int x = dfs(v, u);
if (x)
vec.push_back(x);
}
while (vec.size() > 2) {
int ver1 = vec.back();
vec.pop_back();
int ver2 = vec.back();
vec.pop_back();
swap(resMin[ver1], resMin[ver2]);
ansMin += 4;
}
if (vec.size() == 1) {
swap(resMin[u], resMin[vec[0]]);
ansMin += 2;
return 0;
}
if (vec.size() == 0 && u != 1)
return u;
if (vec.size()) {
int b1 = vec[0], b2 = vec[1];
swap(resMin[u], resMin[b1]);
swap(resMin[u], resMin[b2]);
ansMin += 4;
}
else {
swap(resMin[1], resMin[adj[u][0]]);
ansMin += 2;
}
return 0;
}
void solveMin() {
for (int i = 1; i <= n; i++)
resMin[i] = i;
dfs();
}
void writeOutput() {
cout << ansMin << ' ' << ansMax << '\n';
for (int i = 1; i <= n; i++)
cout << resMin[i] << ' ';
cout << '\n';
for (int i = 1; i <= n; i++)
cout << resMax[i] << ' ';
cout.flush();
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
readInput();
if (n == 1) {
cout << 0 << ' ' << 0 << '\n';
cout << 1 << '\n' << 1;
cout.flush();
return 0;
}
solveMax();
solveMin();
writeOutput();
return 0;
}
Compilation message
Village.cpp: In function 'void calcResMax()':
Village.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = 0; j < base[i].size(); j++)
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
3 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
3 ms |
10684 KB |
Output is correct |
4 |
Correct |
3 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
3 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
3 ms |
10844 KB |
Output is correct |
9 |
Correct |
3 ms |
10844 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
3 ms |
10844 KB |
Output is correct |
12 |
Correct |
3 ms |
11068 KB |
Output is correct |
13 |
Correct |
3 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
3 ms |
10844 KB |
Output is correct |
17 |
Correct |
3 ms |
10924 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
3 ms |
10844 KB |
Output is correct |
20 |
Correct |
3 ms |
10844 KB |
Output is correct |
21 |
Correct |
3 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
3 ms |
10840 KB |
Output is correct |
27 |
Correct |
2 ms |
10844 KB |
Output is correct |
28 |
Correct |
3 ms |
10840 KB |
Output is correct |
29 |
Correct |
3 ms |
10844 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10840 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10840 KB |
Output is correct |
13 |
Correct |
3 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
10844 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
3 ms |
10684 KB |
Output is correct |
21 |
Correct |
3 ms |
10844 KB |
Output is correct |
22 |
Correct |
2 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10844 KB |
Output is correct |
24 |
Correct |
2 ms |
10844 KB |
Output is correct |
25 |
Correct |
3 ms |
10844 KB |
Output is correct |
26 |
Correct |
3 ms |
10844 KB |
Output is correct |
27 |
Correct |
3 ms |
10844 KB |
Output is correct |
28 |
Correct |
3 ms |
10844 KB |
Output is correct |
29 |
Correct |
3 ms |
11068 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
2 ms |
10840 KB |
Output is correct |
33 |
Correct |
3 ms |
10844 KB |
Output is correct |
34 |
Correct |
3 ms |
10924 KB |
Output is correct |
35 |
Correct |
3 ms |
10844 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
3 ms |
10844 KB |
Output is correct |
38 |
Correct |
3 ms |
10844 KB |
Output is correct |
39 |
Correct |
2 ms |
10844 KB |
Output is correct |
40 |
Correct |
3 ms |
10844 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
3 ms |
10840 KB |
Output is correct |
44 |
Correct |
2 ms |
10844 KB |
Output is correct |
45 |
Correct |
3 ms |
10840 KB |
Output is correct |
46 |
Correct |
3 ms |
10844 KB |
Output is correct |
47 |
Correct |
3 ms |
10844 KB |
Output is correct |
48 |
Correct |
47 ms |
16140 KB |
Output is correct |
49 |
Correct |
51 ms |
16748 KB |
Output is correct |
50 |
Correct |
51 ms |
16848 KB |
Output is correct |
51 |
Correct |
40 ms |
15492 KB |
Output is correct |
52 |
Correct |
57 ms |
16576 KB |
Output is correct |
53 |
Correct |
45 ms |
16080 KB |
Output is correct |
54 |
Correct |
37 ms |
18728 KB |
Output is correct |
55 |
Correct |
76 ms |
27288 KB |
Output is correct |
56 |
Correct |
63 ms |
21740 KB |
Output is correct |
57 |
Correct |
66 ms |
20152 KB |
Output is correct |
58 |
Correct |
57 ms |
18412 KB |
Output is correct |
59 |
Correct |
49 ms |
16804 KB |
Output is correct |
60 |
Correct |
66 ms |
26572 KB |
Output is correct |
61 |
Correct |
56 ms |
19144 KB |
Output is correct |
62 |
Correct |
44 ms |
17196 KB |
Output is correct |
63 |
Correct |
42 ms |
16848 KB |
Output is correct |
64 |
Correct |
49 ms |
16980 KB |
Output is correct |
65 |
Correct |
46 ms |
17080 KB |
Output is correct |
66 |
Correct |
51 ms |
17168 KB |
Output is correct |
67 |
Correct |
40 ms |
16724 KB |
Output is correct |
68 |
Correct |
39 ms |
16208 KB |
Output is correct |
69 |
Correct |
64 ms |
17324 KB |
Output is correct |
70 |
Correct |
43 ms |
16716 KB |
Output is correct |
71 |
Correct |
32 ms |
15196 KB |
Output is correct |
72 |
Correct |
36 ms |
15700 KB |
Output is correct |
73 |
Correct |
49 ms |
17356 KB |
Output is correct |
74 |
Correct |
44 ms |
16724 KB |
Output is correct |
75 |
Correct |
49 ms |
16588 KB |
Output is correct |
76 |
Correct |
62 ms |
16720 KB |
Output is correct |
77 |
Correct |
47 ms |
16720 KB |
Output is correct |
78 |
Correct |
34 ms |
14928 KB |
Output is correct |
79 |
Correct |
36 ms |
15740 KB |
Output is correct |
80 |
Correct |
49 ms |
17072 KB |
Output is correct |
81 |
Correct |
48 ms |
16976 KB |
Output is correct |
82 |
Correct |
56 ms |
16980 KB |
Output is correct |