# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 25;
const ll inf = 2e9 + 0;
const ll mod = 998244353;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
ll n, sz[maxn];
vector <ll> g[maxn];
vector <ll> ord;
ll up[maxn][22];
ll tin[maxn];
ll tout[maxn];
ll d[maxn], timer;
ll mx_par[maxn];
ll mn_par[maxn];
ll cnt[maxn];
void dfs_calc (ll v, ll pa)
{
sz[v] = 1;
for (ll to : g[v])
{
if (to != pa)
{
dfs_calc(to, v);
sz[v] += sz[to];
}
}
}
ll find_c (ll v, ll pa)
{
for (ll to : g[v])
{
if (to != pa)
{
if (sz[to] + sz[to] > n)
{
return find_c(to, v);
}
}
}
return v;
}
void dfs_ord (ll v, ll pa)
{
ord.pb(v);
d[v] = d[pa] + 1;
tin[v] = ++timer;
up[v][0] = pa;
for (ll j = 1; j <= 19; ++j)
{
up[v][j] = up[up[v][j - 1]][j - 1];
}
for (ll to : g[v])
{
if (to != pa)
{
dfs_ord(to, v);
}
}
tout[v] = timer;
}
bool is_anc (ll u, ll v)
{
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
ll lca (ll u, ll v)
{
if (is_anc(u, v)) return u;
if (is_anc(v, u)) return v;
for (ll j = 19; j >= 0; --j)
{
if (!is_anc(up[u][j], v))
{
u = up[u][j];
}
}
return up[u][0];
}
void ma1n (/* SABR */)
{
cin >> n;
for (ll i = 1, u, v; i < n; ++i)
{
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
cnt[u]++, cnt[v]++;
}
ll ans_mn = 0, ans_mx = 0;
queue <int> q;
for (ll i = 1; i <= n; ++i)
{
if (cnt[i] == 1)
{
q.push(i);
}
mn_par[i] = i;
}
while (!q.empty())
{
ll v = q.front();
q.pop();
if (mn_par[v] == v)
{
ll qwe = g[v][0];
for (ll to : g[v])
{
if (cnt[to])
{
qwe = to;
break;
}
}
swap(mn_par[v], mn_par[qwe]);
ans_mn += 2;
}
for (ll to : g[v])
{
if (cnt[to])
{
cnt[v]--, cnt[to]--;
if (cnt[to] <= 1)
{
q.push(to);
}
}
}
}
dfs_calc (1, 1);
ll centroid = find_c(1, 1);
dfs_ord(centroid, centroid);
for (ll i = 0; i < sz(ord); ++i)
{
ll u = ord[i], v = ord[(i + (n / 2)) % n];
ans_mx += d[u] + d[v] - d[lca(u, v)] * 2;
mx_par[u] = v;
}
cout << ans_mn << ' ' << ans_mx << nl;
for (ll i = 1; i <= n; ++i)
{
cout << mn_par[i] << ' ';
}
cout << nl;
for (ll i = 1; i <= n; ++i)
{
cout << mx_par[i] << ' ';
}
cout << nl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << ' ';
ma1n();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 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 |
2692 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 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 |
2696 KB |
Output is correct |
11 |
Correct |
1 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2696 KB |
Output is correct |
13 |
Correct |
1 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2692 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
2 ms |
2876 KB |
Output is correct |
3 |
Correct |
2 ms |
2864 KB |
Output is correct |
4 |
Correct |
3 ms |
2900 KB |
Output is correct |
5 |
Correct |
3 ms |
2956 KB |
Output is correct |
6 |
Correct |
2 ms |
2952 KB |
Output is correct |
7 |
Correct |
3 ms |
2948 KB |
Output is correct |
8 |
Correct |
2 ms |
2900 KB |
Output is correct |
9 |
Correct |
2 ms |
2900 KB |
Output is correct |
10 |
Correct |
2 ms |
3028 KB |
Output is correct |
11 |
Correct |
3 ms |
3028 KB |
Output is correct |
12 |
Correct |
2 ms |
3028 KB |
Output is correct |
13 |
Correct |
2 ms |
2956 KB |
Output is correct |
14 |
Correct |
2 ms |
2952 KB |
Output is correct |
15 |
Correct |
2 ms |
2900 KB |
Output is correct |
16 |
Correct |
2 ms |
3028 KB |
Output is correct |
17 |
Correct |
3 ms |
3028 KB |
Output is correct |
18 |
Correct |
2 ms |
2952 KB |
Output is correct |
19 |
Correct |
2 ms |
2956 KB |
Output is correct |
20 |
Correct |
2 ms |
3028 KB |
Output is correct |
21 |
Correct |
2 ms |
3028 KB |
Output is correct |
22 |
Correct |
3 ms |
3028 KB |
Output is correct |
23 |
Correct |
3 ms |
2900 KB |
Output is correct |
24 |
Correct |
2 ms |
2956 KB |
Output is correct |
25 |
Correct |
2 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
3028 KB |
Output is correct |
27 |
Correct |
2 ms |
2812 KB |
Output is correct |
28 |
Correct |
3 ms |
3028 KB |
Output is correct |
29 |
Correct |
3 ms |
3028 KB |
Output is correct |
30 |
Correct |
2 ms |
2952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 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 |
2692 KB |
Output is correct |
6 |
Correct |
2 ms |
2696 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 |
2696 KB |
Output is correct |
11 |
Correct |
1 ms |
2688 KB |
Output is correct |
12 |
Correct |
2 ms |
2696 KB |
Output is correct |
13 |
Correct |
1 ms |
2688 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2692 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2876 KB |
Output is correct |
20 |
Correct |
2 ms |
2864 KB |
Output is correct |
21 |
Correct |
3 ms |
2900 KB |
Output is correct |
22 |
Correct |
3 ms |
2956 KB |
Output is correct |
23 |
Correct |
2 ms |
2952 KB |
Output is correct |
24 |
Correct |
3 ms |
2948 KB |
Output is correct |
25 |
Correct |
2 ms |
2900 KB |
Output is correct |
26 |
Correct |
2 ms |
2900 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
3 ms |
3028 KB |
Output is correct |
29 |
Correct |
2 ms |
3028 KB |
Output is correct |
30 |
Correct |
2 ms |
2956 KB |
Output is correct |
31 |
Correct |
2 ms |
2952 KB |
Output is correct |
32 |
Correct |
2 ms |
2900 KB |
Output is correct |
33 |
Correct |
2 ms |
3028 KB |
Output is correct |
34 |
Correct |
3 ms |
3028 KB |
Output is correct |
35 |
Correct |
2 ms |
2952 KB |
Output is correct |
36 |
Correct |
2 ms |
2956 KB |
Output is correct |
37 |
Correct |
2 ms |
3028 KB |
Output is correct |
38 |
Correct |
2 ms |
3028 KB |
Output is correct |
39 |
Correct |
3 ms |
3028 KB |
Output is correct |
40 |
Correct |
3 ms |
2900 KB |
Output is correct |
41 |
Correct |
2 ms |
2956 KB |
Output is correct |
42 |
Correct |
2 ms |
2772 KB |
Output is correct |
43 |
Correct |
2 ms |
3028 KB |
Output is correct |
44 |
Correct |
2 ms |
2812 KB |
Output is correct |
45 |
Correct |
3 ms |
3028 KB |
Output is correct |
46 |
Correct |
3 ms |
3028 KB |
Output is correct |
47 |
Correct |
2 ms |
2952 KB |
Output is correct |
48 |
Correct |
122 ms |
29712 KB |
Output is correct |
49 |
Correct |
125 ms |
32444 KB |
Output is correct |
50 |
Correct |
141 ms |
32408 KB |
Output is correct |
51 |
Correct |
112 ms |
25788 KB |
Output is correct |
52 |
Correct |
127 ms |
32240 KB |
Output is correct |
53 |
Correct |
126 ms |
29132 KB |
Output is correct |
54 |
Correct |
97 ms |
20004 KB |
Output is correct |
55 |
Correct |
192 ms |
37696 KB |
Output is correct |
56 |
Correct |
190 ms |
35328 KB |
Output is correct |
57 |
Correct |
181 ms |
33996 KB |
Output is correct |
58 |
Correct |
175 ms |
33444 KB |
Output is correct |
59 |
Correct |
134 ms |
32612 KB |
Output is correct |
60 |
Correct |
119 ms |
33420 KB |
Output is correct |
61 |
Correct |
111 ms |
32808 KB |
Output is correct |
62 |
Correct |
105 ms |
32856 KB |
Output is correct |
63 |
Correct |
106 ms |
31024 KB |
Output is correct |
64 |
Correct |
110 ms |
32940 KB |
Output is correct |
65 |
Correct |
105 ms |
33092 KB |
Output is correct |
66 |
Correct |
112 ms |
31072 KB |
Output is correct |
67 |
Correct |
75 ms |
25024 KB |
Output is correct |
68 |
Correct |
89 ms |
28516 KB |
Output is correct |
69 |
Correct |
105 ms |
33240 KB |
Output is correct |
70 |
Correct |
102 ms |
31300 KB |
Output is correct |
71 |
Correct |
72 ms |
23888 KB |
Output is correct |
72 |
Correct |
79 ms |
26688 KB |
Output is correct |
73 |
Correct |
110 ms |
33216 KB |
Output is correct |
74 |
Correct |
100 ms |
30596 KB |
Output is correct |
75 |
Correct |
130 ms |
32912 KB |
Output is correct |
76 |
Correct |
116 ms |
32472 KB |
Output is correct |
77 |
Correct |
116 ms |
31580 KB |
Output is correct |
78 |
Correct |
72 ms |
22716 KB |
Output is correct |
79 |
Correct |
91 ms |
26104 KB |
Output is correct |
80 |
Correct |
125 ms |
33028 KB |
Output is correct |
81 |
Correct |
105 ms |
31684 KB |
Output is correct |
82 |
Correct |
101 ms |
32592 KB |
Output is correct |