#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<ll>g[N];
vector<ll>res_mn, res_mx;
ll ans_mn = 0, ans_mx = 0;
bool used[N];
ll sz[N];
stack<ll>st[N];
bool useds[N];
bool comp(ll x, ll y) {
return st[x].size() < st[y].size();
}
multiset<ll, decltype(&comp)> s(&comp);
void dfs(ll v, ll par = -1) {
for (ll to : g[v]) {
if (to != par) {
dfs(to, v);
}
}
vector<ll>leaves;
for (ll 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 (ll i = 0; i < leaves.size() - 1; i++) {
res_mn[leaves[i]] = leaves[i + 1];
}
res_mn[leaves[leaves.size() - 1]] = v;
for (ll to : leaves) {
used[to] = true;
}
used[v] = true;
}
}
void calc_sz(ll v, ll par = -1) {
sz[v] = 1;
for (ll 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]);
}
}
ll centroid(ll v, ll par = -1) {
for (ll to : g[v]) {
if (to != par) {
if (sz[to] > n / 2) {
return centroid(to, v);
}
}
}
return v;
}
void inch_tenum_es_qci_stacki_mej(ll v, ll par, ll cur) {
if (!useds[v])st[cur].push(v);
for (ll 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;
ll leaf = 0;
for (ll i = 0; i < n - 1; i++) {
ll x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (ll v = 1; v <= n; v++) {
if (g[v].size() == 1) {
leaf = v;
break;
}
}
ll root = g[leaf][0];
res_mn.resize(n + 1, 0);
res_mx.resize(n + 1, 0);
dfs(root);
calc_sz(1);
ll c = centroid(1);
if (n % 2 != 0) {
ll cur_stack = 0;
for (ll to : g[c]) {
inch_tenum_es_qci_stacki_mej(to, c, cur_stack++);
}
ll maxim = 0;
for (ll i = 0; i < cur_stack; i++) {
if (comp(maxim, i)) {
maxim = i;
}
//cout << i << ": " << st[i].size() << endl;
}
ll v1 = st[maxim].top();
st[maxim].pop();
ll smaxim = 0;
for (ll i = 0; i < cur_stack; i++) {
if (comp(smaxim, i) && i != maxim) {
smaxim = i;
}
//cout << i << ": " << st[i].size() << endl;
}
ll 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 (ll i = 0; i < cur_stack; i++) {
while (!st[i].empty()) {
st[i].pop();
}
}
}
ll cur_stack = 0;
for (ll to : g[c]) {
inch_tenum_es_qci_stacki_mej(to, c, cur_stack++);
}
ll minim = cur_stack;
if (!useds[c]) {
st[minim].push(c);
cur_stack++;
}
for (ll i = 0; i < cur_stack; i++) {
if(st[i].size()!=0)s.insert(i);
}
while (!s.empty()) {
//cout << s.size() << endl;
ll bg = *s.rbegin();
s.erase(--s.end());
ll sbg = *s.rbegin();
s.erase(--s.end());
ll v1 = st[bg].top();
ll 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 (ll v = 1; v <= n; v++) {
cout << res_mn[v] << " ";
}
cout << endl;
for (ll 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(ll, ll)':
Village.cpp:67:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (ll i = 0; i < leaves.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
139592 KB |
Output is correct |
2 |
Correct |
81 ms |
139600 KB |
Output is correct |
3 |
Correct |
80 ms |
139532 KB |
Output is correct |
4 |
Correct |
86 ms |
139556 KB |
Output is correct |
5 |
Correct |
81 ms |
139600 KB |
Output is correct |
6 |
Correct |
79 ms |
139536 KB |
Output is correct |
7 |
Correct |
81 ms |
139628 KB |
Output is correct |
8 |
Correct |
83 ms |
139596 KB |
Output is correct |
9 |
Correct |
80 ms |
139584 KB |
Output is correct |
10 |
Correct |
81 ms |
139556 KB |
Output is correct |
11 |
Correct |
81 ms |
139724 KB |
Output is correct |
12 |
Correct |
82 ms |
139548 KB |
Output is correct |
13 |
Correct |
97 ms |
139600 KB |
Output is correct |
14 |
Correct |
84 ms |
139616 KB |
Output is correct |
15 |
Correct |
79 ms |
139540 KB |
Output is correct |
16 |
Correct |
83 ms |
139556 KB |
Output is correct |
17 |
Correct |
79 ms |
139640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
139644 KB |
Output is correct |
2 |
Correct |
81 ms |
139632 KB |
Output is correct |
3 |
Correct |
80 ms |
139680 KB |
Output is correct |
4 |
Correct |
84 ms |
139692 KB |
Output is correct |
5 |
Partially correct |
88 ms |
139692 KB |
Partially correct |
6 |
Correct |
92 ms |
139612 KB |
Output is correct |
7 |
Correct |
80 ms |
139720 KB |
Output is correct |
8 |
Correct |
80 ms |
139676 KB |
Output is correct |
9 |
Correct |
80 ms |
139660 KB |
Output is correct |
10 |
Correct |
89 ms |
139736 KB |
Output is correct |
11 |
Correct |
84 ms |
139660 KB |
Output is correct |
12 |
Correct |
82 ms |
139716 KB |
Output is correct |
13 |
Correct |
81 ms |
139640 KB |
Output is correct |
14 |
Correct |
81 ms |
139664 KB |
Output is correct |
15 |
Correct |
80 ms |
139724 KB |
Output is correct |
16 |
Correct |
79 ms |
139724 KB |
Output is correct |
17 |
Correct |
91 ms |
139720 KB |
Output is correct |
18 |
Correct |
79 ms |
139688 KB |
Output is correct |
19 |
Correct |
80 ms |
139704 KB |
Output is correct |
20 |
Correct |
81 ms |
139728 KB |
Output is correct |
21 |
Correct |
79 ms |
139732 KB |
Output is correct |
22 |
Correct |
81 ms |
139664 KB |
Output is correct |
23 |
Correct |
80 ms |
139648 KB |
Output is correct |
24 |
Correct |
83 ms |
139596 KB |
Output is correct |
25 |
Correct |
82 ms |
139664 KB |
Output is correct |
26 |
Correct |
82 ms |
139716 KB |
Output is correct |
27 |
Correct |
82 ms |
139688 KB |
Output is correct |
28 |
Correct |
85 ms |
139648 KB |
Output is correct |
29 |
Correct |
86 ms |
139656 KB |
Output is correct |
30 |
Correct |
84 ms |
139704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
139592 KB |
Output is correct |
2 |
Correct |
81 ms |
139600 KB |
Output is correct |
3 |
Correct |
80 ms |
139532 KB |
Output is correct |
4 |
Correct |
86 ms |
139556 KB |
Output is correct |
5 |
Correct |
81 ms |
139600 KB |
Output is correct |
6 |
Correct |
79 ms |
139536 KB |
Output is correct |
7 |
Correct |
81 ms |
139628 KB |
Output is correct |
8 |
Correct |
83 ms |
139596 KB |
Output is correct |
9 |
Correct |
80 ms |
139584 KB |
Output is correct |
10 |
Correct |
81 ms |
139556 KB |
Output is correct |
11 |
Correct |
81 ms |
139724 KB |
Output is correct |
12 |
Correct |
82 ms |
139548 KB |
Output is correct |
13 |
Correct |
97 ms |
139600 KB |
Output is correct |
14 |
Correct |
84 ms |
139616 KB |
Output is correct |
15 |
Correct |
79 ms |
139540 KB |
Output is correct |
16 |
Correct |
83 ms |
139556 KB |
Output is correct |
17 |
Correct |
79 ms |
139640 KB |
Output is correct |
18 |
Correct |
82 ms |
139644 KB |
Output is correct |
19 |
Correct |
81 ms |
139632 KB |
Output is correct |
20 |
Correct |
80 ms |
139680 KB |
Output is correct |
21 |
Correct |
84 ms |
139692 KB |
Output is correct |
22 |
Partially correct |
88 ms |
139692 KB |
Partially correct |
23 |
Correct |
92 ms |
139612 KB |
Output is correct |
24 |
Correct |
80 ms |
139720 KB |
Output is correct |
25 |
Correct |
80 ms |
139676 KB |
Output is correct |
26 |
Correct |
80 ms |
139660 KB |
Output is correct |
27 |
Correct |
89 ms |
139736 KB |
Output is correct |
28 |
Correct |
84 ms |
139660 KB |
Output is correct |
29 |
Correct |
82 ms |
139716 KB |
Output is correct |
30 |
Correct |
81 ms |
139640 KB |
Output is correct |
31 |
Correct |
81 ms |
139664 KB |
Output is correct |
32 |
Correct |
80 ms |
139724 KB |
Output is correct |
33 |
Correct |
79 ms |
139724 KB |
Output is correct |
34 |
Correct |
91 ms |
139720 KB |
Output is correct |
35 |
Correct |
79 ms |
139688 KB |
Output is correct |
36 |
Correct |
80 ms |
139704 KB |
Output is correct |
37 |
Correct |
81 ms |
139728 KB |
Output is correct |
38 |
Correct |
79 ms |
139732 KB |
Output is correct |
39 |
Correct |
81 ms |
139664 KB |
Output is correct |
40 |
Correct |
80 ms |
139648 KB |
Output is correct |
41 |
Correct |
83 ms |
139596 KB |
Output is correct |
42 |
Correct |
82 ms |
139664 KB |
Output is correct |
43 |
Correct |
82 ms |
139716 KB |
Output is correct |
44 |
Correct |
82 ms |
139688 KB |
Output is correct |
45 |
Correct |
85 ms |
139648 KB |
Output is correct |
46 |
Correct |
86 ms |
139656 KB |
Output is correct |
47 |
Correct |
84 ms |
139704 KB |
Output is correct |
48 |
Correct |
151 ms |
147260 KB |
Output is correct |
49 |
Correct |
239 ms |
147916 KB |
Output is correct |
50 |
Correct |
190 ms |
147916 KB |
Output is correct |
51 |
Correct |
144 ms |
145996 KB |
Output is correct |
52 |
Correct |
196 ms |
147828 KB |
Output is correct |
53 |
Correct |
154 ms |
147004 KB |
Output is correct |
54 |
Correct |
135 ms |
148152 KB |
Output is correct |
55 |
Correct |
198 ms |
156652 KB |
Output is correct |
56 |
Correct |
212 ms |
152012 KB |
Output is correct |
57 |
Correct |
181 ms |
149796 KB |
Output is correct |
58 |
Correct |
168 ms |
149180 KB |
Output is correct |
59 |
Correct |
170 ms |
148160 KB |
Output is correct |
60 |
Correct |
175 ms |
151976 KB |
Output is correct |
61 |
Correct |
189 ms |
148808 KB |
Output is correct |
62 |
Correct |
159 ms |
148188 KB |
Output is correct |
63 |
Correct |
183 ms |
147744 KB |
Output is correct |
64 |
Correct |
157 ms |
148316 KB |
Output is correct |
65 |
Correct |
201 ms |
148344 KB |
Output is correct |
66 |
Correct |
164 ms |
147880 KB |
Output is correct |
67 |
Correct |
156 ms |
146368 KB |
Output is correct |
68 |
Correct |
163 ms |
147072 KB |
Output is correct |
69 |
Correct |
162 ms |
148500 KB |
Output is correct |
70 |
Correct |
154 ms |
147796 KB |
Output is correct |
71 |
Correct |
138 ms |
145684 KB |
Output is correct |
72 |
Correct |
141 ms |
146628 KB |
Output is correct |
73 |
Correct |
173 ms |
148468 KB |
Output is correct |
74 |
Correct |
148 ms |
147592 KB |
Output is correct |
75 |
Correct |
173 ms |
148392 KB |
Output is correct |
76 |
Correct |
147 ms |
147676 KB |
Output is correct |
77 |
Correct |
153 ms |
148180 KB |
Output is correct |
78 |
Correct |
129 ms |
145280 KB |
Output is correct |
79 |
Correct |
135 ms |
146124 KB |
Output is correct |
80 |
Correct |
170 ms |
148316 KB |
Output is correct |
81 |
Correct |
157 ms |
148256 KB |
Output is correct |
82 |
Correct |
149 ms |
148000 KB |
Output is correct |