#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
ll N;
vvi adj;
// Code for the mini length
vi dpP, dpN;
vi ans_mini;
ll miniCheck = 0;
void dfs_mini(ll v, ll p)
{
if (sz(adj[v]) == 1 && v != p)
{
// leaf
dpN[v] = 0;
dpP[v] = INT_MAX;
return;
}
for (ll u : adj[v])
if (u != p)
dfs_mini(u, v);
dpN[v] = 0;
for (ll u : adj[v])
if (u != p)
dpN[v] += min(dpP[u], dpN[u] + 2);
for (ll u : adj[v])
if (u != p)
{
ll tmp = dpN[v] - min(dpP[u], dpN[u] + 2) + min(dpP[u], dpN[u]) + 2;
dpP[v] = min(dpP[v], tmp);
}
}
void reconstruct_dpP(ll v, ll p);
void reconstruct_dpN(ll v, ll p, ll except);
void reconstruct_dpN(ll v, ll p, ll except = -1)
{
if (sz(adj[v]) == 1 && v != p)
return; // leaf
for (ll u : adj[v])
{
if (u == p)
continue;
if (u == except)
{
swap(ans_mini[v], ans_mini[u]);
miniCheck += 2;
if (dpP[u] < dpN[u])
reconstruct_dpP(u, v);
else
reconstruct_dpN(u, v);
}
else if (dpP[u] < dpN[u] + 2)
reconstruct_dpP(u, v);
else
{
swap(ans_mini[v], ans_mini[u]);
miniCheck += 2;
reconstruct_dpN(u, v);
}
}
}
void reconstruct_dpP(ll v, ll p)
{
if (sz(adj[v]) == 1 && v != p)
return; // leaf
ll w = -1;
for (ll u : adj[v])
if (u != p)
{
ll tmp = dpN[v] - min(dpP[u], dpN[u] + 2) + min(dpP[u], dpN[u]) + 2;
if (dpP[v] == tmp)
{
w = u;
break;
}
}
reconstruct_dpN(v, p, w);
}
// code for the maxi part
vi subsize, swapsRequired;
vvi contains;
vi ans_maxi;
void dfs_maxi(ll v, ll p)
{
subsize[v] = 1;
for (ll u : adj[v])
if (u != p)
{
dfs_maxi(u, v);
subsize[v] += subsize[u];
}
}
ll find_split(ll v)
{ // v must be root of the tree with respect to subsize
pii maxSub = {-1, -1};
for (ll u : adj[v])
maxSub = max(maxSub, {subsize[u], u});
if (maxSub.first <= N / 2)
return v;
subsize[v] = N - maxSub.first;
subsize[maxSub.second] = N;
return find_split(maxSub.second);
}
void collect(ll v, ll p)
{
contains[v].push_back(v);
for (ll u : adj[v])
if (u != p)
{
collect(u, v);
swapsRequired[v] += swapsRequired[u] + subsize[u];
if (v != p)
{
if (sz(contains[u]) > sz(contains[v]))
swap(contains[u], contains[v]);
while (!contains[u].empty())
contains[v].push_back(contains[u].back()), contains[u].pop_back();
}
}
}
void distribute(ll v)
{
vi places;
places.push_back(v);
for (ll u : adj[v])
places.push_back(u);
sort(all(places), [&](ll a, ll b)
{ return sz(contains[a]) > sz(contains[b]); });
queue<ll> todo;
for (ll u : places)
for (ll w : contains[u])
todo.push(w);
for (ll i = 0; i < sz(places); ++i)
for (ll u : contains[places[(i + 1) % sz(places)]])
ans_maxi[u] = todo.front(), todo.pop();
}
// main
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
adj.resize(N);
for (ll i = 0, a, b; i < N - 1; ++i)
{
cin >> a >> b, --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
// mini
dpP.assign(N, INT_MAX), dpN.assign(N, INT_MAX);
dfs_mini(0, 0);
ans_mini.resize(N);
iota(all(ans_mini), 1);
reconstruct_dpP(0, 0);
// maxi
subsize.resize(N);
dfs_maxi(0, 0);
ll center = find_split(0);
contains.resize(N);
swapsRequired.assign(N, 0);
collect(center, center);
ans_maxi.resize(N);
distribute(center);
// ans
cout << dpP[0] << " ";
cout << 2 * swapsRequired[center] << endl;
assert(miniCheck == dpP[0]);
for (ll v : ans_mini)
cout << v << " ";
cout << endl;
for (ll v : ans_maxi)
cout << v + 1 << " ";
cout << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
4 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
468 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
96 ms |
19920 KB |
Output is correct |
49 |
Correct |
138 ms |
21824 KB |
Output is correct |
50 |
Correct |
132 ms |
21760 KB |
Output is correct |
51 |
Correct |
91 ms |
17060 KB |
Output is correct |
52 |
Correct |
112 ms |
21472 KB |
Output is correct |
53 |
Correct |
85 ms |
19624 KB |
Output is correct |
54 |
Correct |
47 ms |
12036 KB |
Output is correct |
55 |
Correct |
139 ms |
24096 KB |
Output is correct |
56 |
Correct |
108 ms |
21684 KB |
Output is correct |
57 |
Correct |
117 ms |
20676 KB |
Output is correct |
58 |
Correct |
118 ms |
20508 KB |
Output is correct |
59 |
Correct |
101 ms |
21148 KB |
Output is correct |
60 |
Correct |
67 ms |
18752 KB |
Output is correct |
61 |
Correct |
78 ms |
18668 KB |
Output is correct |
62 |
Correct |
87 ms |
20080 KB |
Output is correct |
63 |
Correct |
91 ms |
19108 KB |
Output is correct |
64 |
Correct |
100 ms |
21584 KB |
Output is correct |
65 |
Correct |
109 ms |
21148 KB |
Output is correct |
66 |
Correct |
86 ms |
18660 KB |
Output is correct |
67 |
Correct |
70 ms |
14040 KB |
Output is correct |
68 |
Correct |
74 ms |
18028 KB |
Output is correct |
69 |
Correct |
92 ms |
20496 KB |
Output is correct |
70 |
Correct |
97 ms |
19080 KB |
Output is correct |
71 |
Correct |
65 ms |
14228 KB |
Output is correct |
72 |
Correct |
85 ms |
16124 KB |
Output is correct |
73 |
Correct |
111 ms |
20388 KB |
Output is correct |
74 |
Correct |
82 ms |
18604 KB |
Output is correct |
75 |
Correct |
126 ms |
23684 KB |
Output is correct |
76 |
Correct |
114 ms |
23548 KB |
Output is correct |
77 |
Correct |
88 ms |
21196 KB |
Output is correct |
78 |
Correct |
61 ms |
15256 KB |
Output is correct |
79 |
Correct |
76 ms |
16836 KB |
Output is correct |
80 |
Correct |
165 ms |
23800 KB |
Output is correct |
81 |
Correct |
105 ms |
20820 KB |
Output is correct |
82 |
Correct |
82 ms |
19356 KB |
Output is correct |