#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
#include <ctime>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <limits>
#include <iomanip>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <functional>
#include <random>
#include <cassert>
using namespace std;
typedef long long ll;
typedef long double ld;
#define ff first
#define ss second
ll ttt;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 200007;
const ll M = 307;
const ll LG = 31;
ll n, m, k;
vector<int>g[N];
vector<int>res_mn, res_mx;
ll ans_mn = 0, ans_mx = 0;
bool used[N];
int sz[N];
stack<int>st[N];
bool useds[N];
bool comp(int x, int y) {
return st[x].size() < st[y].size();
}
multiset<int, decltype(&comp)> s(&comp);
void dfs(int v, int par = -1) {
for (int to : g[v]) {
if (to != par) {
dfs(to, v);
}
}
vector<int>leaves;
for (int to : g[v]) {
if (to != par) {
if (!used[to]) {
leaves.push_back(to);
}
}
}
if (leaves.size() != 0) {
ans_mn += leaves.size() * 2;
res_mn[v] = leaves[0];
for (int i = 0; i < leaves.size() - 1; i++) {
res_mn[leaves[i]] = leaves[i + 1];
}
res_mn[leaves[leaves.size() - 1]] = v;
for (int to : leaves) {
used[to] = true;
}
used[v] = true;
}
}
void calc_sz(int v, int par = -1) {
sz[v] = 1;
for (int to : g[v]) {
if (to != par) {
calc_sz(to, v);
sz[v] += sz[to];
}
}
if (par != -1) {
ans_mx += 2 * min(ll(sz[v]), n - sz[v]);
}
}
int centroid(int v, int par = -1) {
for (int to : g[v]) {
if (to != par) {
if (sz[to] > n / 2) {
return centroid(to, v);
}
}
}
return v;
}
void inch_tenum_es_qci_stacki_mej(int v, int par, int cur) {
if (!useds[v])st[cur].push(v);
for (int to : g[v]) {
if (to != par) {
inch_tenum_es_qci_stacki_mej(to, v, cur);
}
}
}
int main() {
//freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
int leaf = 0;
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (int v = 1; v <= n; v++) {
if (g[v].size() == 1) {
leaf = v;
break;
}
}
int root = g[leaf][0];
res_mn.resize(n + 1, 0);
res_mx.resize(n + 1, 0);
dfs(root);
calc_sz(1);
int c = centroid(1);
if (n % 2 != 0) {
int cur_stack = 0;
for (int to : g[c]) {
inch_tenum_es_qci_stacki_mej(to, c, cur_stack++);
}
int maxim = 0;
for (int i = 0; i < cur_stack; i++) {
if (comp(maxim, i)) {
maxim = i;
}
//cout << i << ": " << st[i].size() << endl;
}
int v1 = st[maxim].top();
st[maxim].pop();
int smaxim = 0;
for (int i = 0; i < cur_stack; i++) {
if (comp(smaxim, i) && i != maxim) {
smaxim = i;
}
//cout << i << ": " << st[i].size() << endl;
}
int v2 = st[smaxim].top();
st[smaxim].pop();
res_mx[c] = v1;
res_mx[v1] = v2;
res_mx[v2] = c;
useds[c] = useds[v2] = useds[v1] = true;
for (int i = 0; i < cur_stack; i++) {
while (!st[i].empty()) {
st[i].pop();
}
}
}
int cur_stack = 0;
for (int to : g[c]) {
inch_tenum_es_qci_stacki_mej(to, c, cur_stack++);
}
int minim = cur_stack;
if (!useds[c]) {
st[minim].push(c);
cur_stack++;
}
for (int i = 0; i < cur_stack; i++) {
if(st[i].size()!=0)s.insert(i);
}
while (!s.empty()) {
//cout << s.size() << endl;
int bg = *s.rbegin();
s.erase(--s.end());
int sbg = *s.rbegin();
s.erase(--s.end());
int v1 = st[bg].top();
int v2 = st[sbg].top();
res_mx[v1] = v2;
res_mx[v2] = v1;
st[bg].pop();
st[sbg].pop();
if (st[bg].size() != 0) {
s.insert(bg);
}
if (st[sbg].size() != 0) {
s.insert(sbg);
}
//cout << bg << " " << sbg << endl;
}
cout << ans_mn << " " << ans_mx << endl;
for (int v = 1; v <= n; v++) {
cout << res_mn[v] << " ";
}
cout << endl;
for (int v = 1; v <= n; v++) {
cout << res_mx[v] << " ";
}
cout << endl;
}
/// ---- - -------- ------ -------- -- - - -
/// Just a reminder. Ubuntu password is I O I
/// ---- - -------- ------ -------- -- - - -
Compilation message
Village.cpp: In function 'void dfs(int, int)':
Village.cpp:67:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < leaves.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
139596 KB |
Output is correct |
2 |
Correct |
80 ms |
139560 KB |
Output is correct |
3 |
Correct |
79 ms |
139596 KB |
Output is correct |
4 |
Correct |
81 ms |
139656 KB |
Output is correct |
5 |
Correct |
86 ms |
139544 KB |
Output is correct |
6 |
Correct |
85 ms |
139576 KB |
Output is correct |
7 |
Correct |
78 ms |
139556 KB |
Output is correct |
8 |
Correct |
81 ms |
139620 KB |
Output is correct |
9 |
Correct |
81 ms |
139608 KB |
Output is correct |
10 |
Correct |
87 ms |
139644 KB |
Output is correct |
11 |
Correct |
85 ms |
139596 KB |
Output is correct |
12 |
Correct |
80 ms |
139648 KB |
Output is correct |
13 |
Correct |
83 ms |
139604 KB |
Output is correct |
14 |
Correct |
85 ms |
139564 KB |
Output is correct |
15 |
Correct |
100 ms |
139568 KB |
Output is correct |
16 |
Correct |
81 ms |
139596 KB |
Output is correct |
17 |
Correct |
83 ms |
139564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
139588 KB |
Output is correct |
2 |
Correct |
94 ms |
139568 KB |
Output is correct |
3 |
Correct |
90 ms |
139688 KB |
Output is correct |
4 |
Correct |
84 ms |
139600 KB |
Output is correct |
5 |
Partially correct |
82 ms |
139596 KB |
Partially correct |
6 |
Correct |
82 ms |
139664 KB |
Output is correct |
7 |
Correct |
90 ms |
139768 KB |
Output is correct |
8 |
Correct |
90 ms |
139664 KB |
Output is correct |
9 |
Correct |
87 ms |
139592 KB |
Output is correct |
10 |
Correct |
90 ms |
139636 KB |
Output is correct |
11 |
Correct |
89 ms |
139644 KB |
Output is correct |
12 |
Correct |
99 ms |
139720 KB |
Output is correct |
13 |
Correct |
86 ms |
139672 KB |
Output is correct |
14 |
Correct |
82 ms |
139668 KB |
Output is correct |
15 |
Correct |
81 ms |
139660 KB |
Output is correct |
16 |
Correct |
87 ms |
139636 KB |
Output is correct |
17 |
Correct |
93 ms |
139712 KB |
Output is correct |
18 |
Correct |
87 ms |
139592 KB |
Output is correct |
19 |
Correct |
95 ms |
139728 KB |
Output is correct |
20 |
Correct |
94 ms |
139624 KB |
Output is correct |
21 |
Correct |
100 ms |
139648 KB |
Output is correct |
22 |
Correct |
107 ms |
139624 KB |
Output is correct |
23 |
Correct |
92 ms |
139628 KB |
Output is correct |
24 |
Correct |
90 ms |
139644 KB |
Output is correct |
25 |
Correct |
95 ms |
139680 KB |
Output is correct |
26 |
Correct |
98 ms |
139644 KB |
Output is correct |
27 |
Correct |
106 ms |
139648 KB |
Output is correct |
28 |
Correct |
92 ms |
139596 KB |
Output is correct |
29 |
Correct |
89 ms |
139632 KB |
Output is correct |
30 |
Correct |
88 ms |
139708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
139596 KB |
Output is correct |
2 |
Correct |
80 ms |
139560 KB |
Output is correct |
3 |
Correct |
79 ms |
139596 KB |
Output is correct |
4 |
Correct |
81 ms |
139656 KB |
Output is correct |
5 |
Correct |
86 ms |
139544 KB |
Output is correct |
6 |
Correct |
85 ms |
139576 KB |
Output is correct |
7 |
Correct |
78 ms |
139556 KB |
Output is correct |
8 |
Correct |
81 ms |
139620 KB |
Output is correct |
9 |
Correct |
81 ms |
139608 KB |
Output is correct |
10 |
Correct |
87 ms |
139644 KB |
Output is correct |
11 |
Correct |
85 ms |
139596 KB |
Output is correct |
12 |
Correct |
80 ms |
139648 KB |
Output is correct |
13 |
Correct |
83 ms |
139604 KB |
Output is correct |
14 |
Correct |
85 ms |
139564 KB |
Output is correct |
15 |
Correct |
100 ms |
139568 KB |
Output is correct |
16 |
Correct |
81 ms |
139596 KB |
Output is correct |
17 |
Correct |
83 ms |
139564 KB |
Output is correct |
18 |
Correct |
85 ms |
139588 KB |
Output is correct |
19 |
Correct |
94 ms |
139568 KB |
Output is correct |
20 |
Correct |
90 ms |
139688 KB |
Output is correct |
21 |
Correct |
84 ms |
139600 KB |
Output is correct |
22 |
Partially correct |
82 ms |
139596 KB |
Partially correct |
23 |
Correct |
82 ms |
139664 KB |
Output is correct |
24 |
Correct |
90 ms |
139768 KB |
Output is correct |
25 |
Correct |
90 ms |
139664 KB |
Output is correct |
26 |
Correct |
87 ms |
139592 KB |
Output is correct |
27 |
Correct |
90 ms |
139636 KB |
Output is correct |
28 |
Correct |
89 ms |
139644 KB |
Output is correct |
29 |
Correct |
99 ms |
139720 KB |
Output is correct |
30 |
Correct |
86 ms |
139672 KB |
Output is correct |
31 |
Correct |
82 ms |
139668 KB |
Output is correct |
32 |
Correct |
81 ms |
139660 KB |
Output is correct |
33 |
Correct |
87 ms |
139636 KB |
Output is correct |
34 |
Correct |
93 ms |
139712 KB |
Output is correct |
35 |
Correct |
87 ms |
139592 KB |
Output is correct |
36 |
Correct |
95 ms |
139728 KB |
Output is correct |
37 |
Correct |
94 ms |
139624 KB |
Output is correct |
38 |
Correct |
100 ms |
139648 KB |
Output is correct |
39 |
Correct |
107 ms |
139624 KB |
Output is correct |
40 |
Correct |
92 ms |
139628 KB |
Output is correct |
41 |
Correct |
90 ms |
139644 KB |
Output is correct |
42 |
Correct |
95 ms |
139680 KB |
Output is correct |
43 |
Correct |
98 ms |
139644 KB |
Output is correct |
44 |
Correct |
106 ms |
139648 KB |
Output is correct |
45 |
Correct |
92 ms |
139596 KB |
Output is correct |
46 |
Correct |
89 ms |
139632 KB |
Output is correct |
47 |
Correct |
88 ms |
139708 KB |
Output is correct |
48 |
Correct |
196 ms |
145616 KB |
Output is correct |
49 |
Correct |
190 ms |
145968 KB |
Output is correct |
50 |
Correct |
216 ms |
146124 KB |
Output is correct |
51 |
Correct |
172 ms |
144688 KB |
Output is correct |
52 |
Correct |
191 ms |
145924 KB |
Output is correct |
53 |
Correct |
168 ms |
145292 KB |
Output is correct |
54 |
Correct |
147 ms |
148348 KB |
Output is correct |
55 |
Correct |
226 ms |
156788 KB |
Output is correct |
56 |
Correct |
209 ms |
150916 KB |
Output is correct |
57 |
Correct |
176 ms |
148296 KB |
Output is correct |
58 |
Correct |
210 ms |
147648 KB |
Output is correct |
59 |
Correct |
183 ms |
146156 KB |
Output is correct |
60 |
Correct |
179 ms |
150496 KB |
Output is correct |
61 |
Correct |
193 ms |
147252 KB |
Output is correct |
62 |
Correct |
172 ms |
146552 KB |
Output is correct |
63 |
Correct |
165 ms |
145996 KB |
Output is correct |
64 |
Correct |
172 ms |
146340 KB |
Output is correct |
65 |
Correct |
169 ms |
146452 KB |
Output is correct |
66 |
Correct |
177 ms |
146104 KB |
Output is correct |
67 |
Correct |
149 ms |
145172 KB |
Output is correct |
68 |
Correct |
150 ms |
145380 KB |
Output is correct |
69 |
Correct |
166 ms |
146472 KB |
Output is correct |
70 |
Correct |
164 ms |
146040 KB |
Output is correct |
71 |
Correct |
144 ms |
144560 KB |
Output is correct |
72 |
Correct |
166 ms |
145096 KB |
Output is correct |
73 |
Correct |
155 ms |
146492 KB |
Output is correct |
74 |
Correct |
169 ms |
145860 KB |
Output is correct |
75 |
Correct |
198 ms |
145928 KB |
Output is correct |
76 |
Correct |
177 ms |
145916 KB |
Output is correct |
77 |
Correct |
177 ms |
146124 KB |
Output is correct |
78 |
Correct |
134 ms |
144076 KB |
Output is correct |
79 |
Correct |
190 ms |
144696 KB |
Output is correct |
80 |
Correct |
155 ms |
145868 KB |
Output is correct |
81 |
Correct |
157 ms |
146300 KB |
Output is correct |
82 |
Correct |
158 ms |
146244 KB |
Output is correct |