#include "bits/extc++.h"
using namespace std;
template <typename T>
void dbgh(const T& t) {
cerr << t << endl;
}
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t << " | ";
dbgh(u...);
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
<< ": "; \
dbgh(__VA_ARGS__)
#else
#define cerr \
if (false) \
cerr
#define dbg(...)
#endif
#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())
const int maxn = 1e5 + 5;
vector<int> graph[maxn];
void pdfs(int u) {
for (auto& v : graph[u]) {
graph[v].erase(find(begin(graph[v]), end(graph[v]), u));
pdfs(v);
}
}
namespace minv {
int memo[maxn][2];
vector<int> res, choice[maxn][2];
int dp(int u, bool b) {
int& ans = memo[u][b];
if (ans != -1) {
return ans;
}
int m = sz(graph[u]);
int cdp[m + 1][2], from[m + 1][2];
memset(cdp, 0x3f, sizeof(cdp));
cdp[0][0] = 0;
for (int k = 0; k < m; k++) {
int v = graph[u][k];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
int cv = cdp[k][i] + dp(v, j) + (j ? 2 : 0);
if (cv < cdp[k + 1][i || j]) {
cdp[k + 1][i || j] = cv;
from[k + 1][i || j] = (i << 1) | j;
}
}
}
}
int j = 1;
if (b) {
if (cdp[m][0] < cdp[m][1]) {
j = 0;
}
}
ans = cdp[m][j];
auto& c = choice[u][b];
for (int i = m; i >= 1; i--) {
int ci = from[i][j] >> 1, cj = from[i][j] & 1;
c.push_back(cj);
j = ci;
}
reverse(begin(c), end(c));
return ans;
}
void cdp(int u, bool b) {
int m = sz(graph[u]);
for (int i = 0; i < m; i++) {
int v = graph[u][i];
if (choice[u][b][i]) {
swap(res[u], res[v]);
cdp(v, true);
} else {
cdp(v, false);
}
}
}
pair<int, vector<int>> solve(int n) {
memset(memo, -1, sizeof(memo));
dp(0, false);
res = vector<int>(n);
iota(begin(res), end(res), 0);
cdp(0, false);
return {dp(0, false), res};
}
} // namespace minv
namespace maxv {
long ans = 0;
int n, siz[maxn];
bool cans, vis[maxn];
vector<int> res, comp;
void dfs1(int u, int p = -1) {
if (vis[u]) {
siz[u] = 0;
return;
}
siz[u] = 1;
for (auto& v : graph[u]) {
if (v != p) {
dfs1(v, u);
siz[u] += siz[v];
if (cans) {
ans += min(siz[v], n - siz[v]);
}
}
}
}
void dfs2(int u) {
if (vis[u]) {
return;
}
vis[u] = true;
comp.push_back(u);
for (auto& v : graph[u]) {
dfs2(v);
}
}
int centroid(int u) {
while (true) {
pair<int, int> opt {0, 0};
for (auto& v : graph[u]) {
opt = max(opt, pair<int, int> {siz[v], v});
}
if (opt.first > n / 2) {
int v = opt.second;
siz[u] -= siz[v];
siz[v] += siz[u];
u = v;
} else {
break;
}
}
return u;
}
pair<long, vector<int>> solve(int _n) {
n = _n;
res = vector<int>(n);
cans = true;
dfs1(0);
cans = false;
int c = centroid(0);
dbg(c);
vector<vector<int>> groups;
groups.push_back({c});
vis[c] = true;
for (auto& v : graph[c]) {
comp.clear();
dfs2(v);
groups.push_back(comp);
}
sort(begin(groups), end(groups), [&](const auto &a, const auto &b) -> bool {
return sz(a) > sz(b);
});
int m = sz(groups);
vector<int> ngroups[m];
for (int i = 0; i < m; i++) {
dbg(sz(groups[i]));
int j = i + 1;
if (j == m) {
j = 0;
}
for (auto& a : groups[i]) {
while (sz(ngroups[j]) == sz(groups[j])) {
j++;
if (j == m) {
j = 0;
}
}
assert(i != j);
ngroups[j].push_back(a);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < sz(groups[i]); j++) {
res[groups[i][j]] = ngroups[i][j];
}
}
return {ans * 2, res};
}
} // namespace maxv
void solve() {
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
auto [mx, mxa] = maxv::solve(n);
pdfs(0);
auto [mn, mna] = minv::solve(n);
cout << mn << " " << mx << endl;
for (int i = 0; i < n; i++) {
cout << mna[i] + 1 << " \n"[i == n - 1];
}
for (int i = 0; i < n; i++) {
cout << mxa[i] + 1 << " \n"[i == n - 1];
}
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cin.exceptions(ios::failbit);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8100 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
6 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8096 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
5 ms |
8156 KB |
Output is correct |
5 |
Correct |
5 ms |
8152 KB |
Output is correct |
6 |
Correct |
6 ms |
8156 KB |
Output is correct |
7 |
Correct |
6 ms |
8408 KB |
Output is correct |
8 |
Correct |
6 ms |
8284 KB |
Output is correct |
9 |
Correct |
5 ms |
8276 KB |
Output is correct |
10 |
Correct |
6 ms |
8284 KB |
Output is correct |
11 |
Correct |
5 ms |
8276 KB |
Output is correct |
12 |
Correct |
5 ms |
8276 KB |
Output is correct |
13 |
Correct |
6 ms |
8276 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
5 ms |
8156 KB |
Output is correct |
17 |
Correct |
5 ms |
8148 KB |
Output is correct |
18 |
Correct |
6 ms |
8172 KB |
Output is correct |
19 |
Correct |
5 ms |
8152 KB |
Output is correct |
20 |
Correct |
5 ms |
8148 KB |
Output is correct |
21 |
Correct |
5 ms |
8236 KB |
Output is correct |
22 |
Correct |
5 ms |
8232 KB |
Output is correct |
23 |
Correct |
5 ms |
8148 KB |
Output is correct |
24 |
Correct |
5 ms |
8148 KB |
Output is correct |
25 |
Correct |
5 ms |
8148 KB |
Output is correct |
26 |
Correct |
6 ms |
8148 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
5 ms |
8148 KB |
Output is correct |
29 |
Correct |
5 ms |
8136 KB |
Output is correct |
30 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8148 KB |
Output is correct |
2 |
Correct |
5 ms |
8148 KB |
Output is correct |
3 |
Correct |
4 ms |
8148 KB |
Output is correct |
4 |
Correct |
4 ms |
8148 KB |
Output is correct |
5 |
Correct |
4 ms |
8100 KB |
Output is correct |
6 |
Correct |
4 ms |
8148 KB |
Output is correct |
7 |
Correct |
6 ms |
8148 KB |
Output is correct |
8 |
Correct |
4 ms |
8148 KB |
Output is correct |
9 |
Correct |
4 ms |
8148 KB |
Output is correct |
10 |
Correct |
4 ms |
8148 KB |
Output is correct |
11 |
Correct |
4 ms |
8148 KB |
Output is correct |
12 |
Correct |
4 ms |
8148 KB |
Output is correct |
13 |
Correct |
4 ms |
8148 KB |
Output is correct |
14 |
Correct |
5 ms |
8148 KB |
Output is correct |
15 |
Correct |
5 ms |
8148 KB |
Output is correct |
16 |
Correct |
4 ms |
8096 KB |
Output is correct |
17 |
Correct |
4 ms |
8148 KB |
Output is correct |
18 |
Correct |
4 ms |
8148 KB |
Output is correct |
19 |
Correct |
5 ms |
8148 KB |
Output is correct |
20 |
Correct |
4 ms |
8148 KB |
Output is correct |
21 |
Correct |
5 ms |
8156 KB |
Output is correct |
22 |
Correct |
5 ms |
8152 KB |
Output is correct |
23 |
Correct |
6 ms |
8156 KB |
Output is correct |
24 |
Correct |
6 ms |
8408 KB |
Output is correct |
25 |
Correct |
6 ms |
8284 KB |
Output is correct |
26 |
Correct |
5 ms |
8276 KB |
Output is correct |
27 |
Correct |
6 ms |
8284 KB |
Output is correct |
28 |
Correct |
5 ms |
8276 KB |
Output is correct |
29 |
Correct |
5 ms |
8276 KB |
Output is correct |
30 |
Correct |
6 ms |
8276 KB |
Output is correct |
31 |
Correct |
5 ms |
8148 KB |
Output is correct |
32 |
Correct |
5 ms |
8148 KB |
Output is correct |
33 |
Correct |
5 ms |
8156 KB |
Output is correct |
34 |
Correct |
5 ms |
8148 KB |
Output is correct |
35 |
Correct |
6 ms |
8172 KB |
Output is correct |
36 |
Correct |
5 ms |
8152 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
8236 KB |
Output is correct |
39 |
Correct |
5 ms |
8232 KB |
Output is correct |
40 |
Correct |
5 ms |
8148 KB |
Output is correct |
41 |
Correct |
5 ms |
8148 KB |
Output is correct |
42 |
Correct |
5 ms |
8148 KB |
Output is correct |
43 |
Correct |
6 ms |
8148 KB |
Output is correct |
44 |
Correct |
5 ms |
8148 KB |
Output is correct |
45 |
Correct |
5 ms |
8148 KB |
Output is correct |
46 |
Correct |
5 ms |
8136 KB |
Output is correct |
47 |
Correct |
5 ms |
8148 KB |
Output is correct |
48 |
Correct |
96 ms |
17940 KB |
Output is correct |
49 |
Correct |
117 ms |
18916 KB |
Output is correct |
50 |
Correct |
106 ms |
18980 KB |
Output is correct |
51 |
Correct |
88 ms |
16580 KB |
Output is correct |
52 |
Correct |
106 ms |
18792 KB |
Output is correct |
53 |
Correct |
96 ms |
17828 KB |
Output is correct |
54 |
Correct |
66 ms |
23476 KB |
Output is correct |
55 |
Correct |
208 ms |
40400 KB |
Output is correct |
56 |
Correct |
152 ms |
29632 KB |
Output is correct |
57 |
Correct |
131 ms |
26308 KB |
Output is correct |
58 |
Correct |
127 ms |
23352 KB |
Output is correct |
59 |
Correct |
111 ms |
19112 KB |
Output is correct |
60 |
Correct |
95 ms |
24152 KB |
Output is correct |
61 |
Correct |
296 ms |
18536 KB |
Output is correct |
62 |
Correct |
77 ms |
17488 KB |
Output is correct |
63 |
Correct |
74 ms |
16996 KB |
Output is correct |
64 |
Correct |
85 ms |
17560 KB |
Output is correct |
65 |
Correct |
85 ms |
17620 KB |
Output is correct |
66 |
Correct |
76 ms |
17128 KB |
Output is correct |
67 |
Correct |
144 ms |
15700 KB |
Output is correct |
68 |
Correct |
71 ms |
16108 KB |
Output is correct |
69 |
Correct |
78 ms |
17552 KB |
Output is correct |
70 |
Correct |
78 ms |
16996 KB |
Output is correct |
71 |
Correct |
51 ms |
14600 KB |
Output is correct |
72 |
Correct |
56 ms |
15532 KB |
Output is correct |
73 |
Correct |
97 ms |
17616 KB |
Output is correct |
74 |
Correct |
66 ms |
16688 KB |
Output is correct |
75 |
Correct |
107 ms |
18908 KB |
Output is correct |
76 |
Correct |
104 ms |
17812 KB |
Output is correct |
77 |
Correct |
80 ms |
17572 KB |
Output is correct |
78 |
Correct |
62 ms |
14376 KB |
Output is correct |
79 |
Correct |
71 ms |
15188 KB |
Output is correct |
80 |
Correct |
122 ms |
18844 KB |
Output is correct |
81 |
Correct |
80 ms |
17540 KB |
Output is correct |
82 |
Correct |
74 ms |
16932 KB |
Output is correct |