#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
#define pb push_back
#define fi first
#define si second
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef tuple<int,int,int> ti;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 100010;
int n, c, cnt;
vector<int> g[MAXN], nodes;
int minans, maxans;
int par[MAXN];
int sz[MAXN];
int vis[MAXN];
int pos[MAXN], dep[MAXN];
int mnc[MAXN], mxc[MAXN];
vector<int> order;
void getsz(int x, int fa) {
sz[x] = 1;
par[x] = fa;
dep[x] = dep[fa] + 1;
for (auto i: g[x]) {
if (i == fa)
continue;
getsz(i, x);
sz[x] += sz[i];
}
}
int centroid(int x, int fa) {
for (auto i: g[x]) {
if (i == fa)
continue;
if (sz[i] * 2 > n)
return centroid(i, x);
}
return x;
}
void dfs(int x, int fa) {
order.pb(x);
for (auto i: g[x]) {
if (i == fa)
continue;
dfs(i, x);
}
}
signed main() {
fast;
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
getsz(1, 1);
c = centroid(1, 1);
// Minimum
for (int i = 1; i <= n; ++i) {
nodes.pb(i);
pos[i] = i;
}
sort(nodes.begin(), nodes.end(), [](int a, int b) {
return sz[a] < sz[b];
});
for (auto i: nodes) {
if (vis[i])
continue;
bool ok = false;
for (auto j: g[i]) {
if (vis[j])
continue;
minans += 2, vis[j] = 1, ok = true;
swap(pos[i], pos[j]);
break;
}
if (!ok) {
minans += 2;
swap(pos[i], pos[par[i]]);
if (i == par[i])
swap(pos[i], pos[g[i].back()]);
}
vis[i] = 1;
}
for (int i = 1; i <= n; ++i)
mnc[pos[i]] = i;
// Maximum
getsz(c, c);
for (int i = 1; i <= n; ++i) {
for (auto j: g[i]) {
int hi = i, lo = j;
if (dep[i] > dep[j])
swap(lo, hi);
maxans += min(n - sz[lo] + 1, sz[lo]);
}
}
dfs(c, c);
for (int i = 0; i < n; ++i) {
int u = order[i], v = order[(i + n / 2) % n];
mxc[u] = v;
}
cout << minans << ' ' << maxans << '\n';
for (int i = 1; i <= n; ++i)
cout << mnc[i] << ' ';
cout << '\n';
for (int i = 1; i <= n; ++i)
cout << mxc[i] << ' ';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2772 KB |
Output is correct |
3 |
Correct |
2 ms |
2772 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2764 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
4 ms |
2772 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
15 |
Correct |
1 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2764 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
2 ms |
2772 KB |
Output is correct |
31 |
Correct |
2 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
4 ms |
2772 KB |
Output is correct |
36 |
Correct |
3 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
3 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
2 ms |
2772 KB |
Output is correct |
46 |
Correct |
2 ms |
2772 KB |
Output is correct |
47 |
Correct |
2 ms |
2772 KB |
Output is correct |
48 |
Correct |
76 ms |
13764 KB |
Output is correct |
49 |
Correct |
102 ms |
14820 KB |
Output is correct |
50 |
Correct |
83 ms |
14772 KB |
Output is correct |
51 |
Correct |
63 ms |
12092 KB |
Output is correct |
52 |
Correct |
98 ms |
14696 KB |
Output is correct |
53 |
Correct |
74 ms |
13504 KB |
Output is correct |
54 |
Correct |
48 ms |
11252 KB |
Output is correct |
55 |
Correct |
116 ms |
20320 KB |
Output is correct |
56 |
Correct |
98 ms |
17504 KB |
Output is correct |
57 |
Correct |
96 ms |
16504 KB |
Output is correct |
58 |
Correct |
94 ms |
15740 KB |
Output is correct |
59 |
Correct |
76 ms |
14908 KB |
Output is correct |
60 |
Correct |
52 ms |
14928 KB |
Output is correct |
61 |
Correct |
57 ms |
15000 KB |
Output is correct |
62 |
Correct |
59 ms |
15140 KB |
Output is correct |
63 |
Correct |
57 ms |
14420 KB |
Output is correct |
64 |
Correct |
73 ms |
15168 KB |
Output is correct |
65 |
Correct |
62 ms |
15296 KB |
Output is correct |
66 |
Correct |
56 ms |
14464 KB |
Output is correct |
67 |
Correct |
43 ms |
11752 KB |
Output is correct |
68 |
Correct |
52 ms |
13444 KB |
Output is correct |
69 |
Correct |
62 ms |
15376 KB |
Output is correct |
70 |
Correct |
54 ms |
14616 KB |
Output is correct |
71 |
Correct |
40 ms |
11552 KB |
Output is correct |
72 |
Correct |
45 ms |
12616 KB |
Output is correct |
73 |
Correct |
59 ms |
15304 KB |
Output is correct |
74 |
Correct |
52 ms |
14224 KB |
Output is correct |
75 |
Correct |
81 ms |
15292 KB |
Output is correct |
76 |
Correct |
87 ms |
14680 KB |
Output is correct |
77 |
Correct |
69 ms |
14840 KB |
Output is correct |
78 |
Correct |
43 ms |
11020 KB |
Output is correct |
79 |
Correct |
49 ms |
12204 KB |
Output is correct |
80 |
Correct |
79 ms |
15296 KB |
Output is correct |
81 |
Correct |
65 ms |
14972 KB |
Output is correct |
82 |
Correct |
63 ms |
14932 KB |
Output is correct |