#include <algorithm>
#include <iostream>
#include <climits>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using ull = unsigned long long;
using ll = long long;
//#define int __int128
//#define int ll
#define pii pair <int, int>
#define all(a) (a).begin(), (a).end()
#define fr first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vt vector
#define FOR(a, b) for(int i = (a); i <= (b); i++)
#define FORr(a, b) for(int i = (a); i >= (b); i--)
#define sz(x) (int)(x).size()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rangerng(int l, int r){
return uniform_int_distribution<>(l, r)(rng);
}
////////////////////////////////////////////////////////////////////////////////////
const int NMAX = 1e5;
int n;
vt <int> adj[NMAX + 1];
int minAns = 0;
int min_pos[NMAX + 1];
void dfs1(int node, int par){
for(int vec : adj[node]){
if(vec != par){
dfs1(vec, node);
}
}
if(min_pos[node] == node && node != 1){
swap(min_pos[node], min_pos[par]);
minAns += 2;
}
}
int maxAns = 0;
int max_pos[NMAX + 1];
int big[NMAX + 1];
void dfs2(int node, int par){
big[node] = 1;
for(int vec : adj[node]){
if(vec != par){
dfs2(vec, node);
big[node] += big[vec];
}
}
maxAns += min(big[node], n - big[node]);
}
int get_centroid(int node, int par){
for(int vec : adj[node]){
if(vec != par && big[vec] > n / 2){
return get_centroid(vec, node);
}
}
return node;
}
vt <int> aux;
void dfs3(int node, int par){
aux.pb(node);
for(int vec : adj[node]){
if(vec != par){
dfs3(vec, node);
}
}
}
void solve(){
cin >> n;
for(int i = 1; i <= n - 1; i++){
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 1; i <= n; i++){
min_pos[i] = i;
}
dfs1(1, 0);
if(min_pos[1] == 1){
int x = adj[1][0];
swap(min_pos[x], min_pos[1]);
minAns += 2;
}
dfs2(1, 0);
int centroid = get_centroid(1, 0);
for(int i = 1; i <= n; i++){
max_pos[i] = i;
}
vt <vt <int>> comps;
for(int vec : adj[centroid]){
aux.clear();
dfs3(vec, centroid);
comps.pb(aux);
}
comps.pb({centroid});
for(int i = 0; i + 1 < sz(comps); i++){
while(sz(comps[i]) > 0 && sz(comps[i + 1]) > 0){
swap(max_pos[comps[i].back()], max_pos[comps[i + 1].back()]);
comps[i].pop_back();
comps[i + 1].pop_back();
}
if(sz(comps[i]) > 0){
swap(comps[i], comps[i + 1]);
}
}
//assert(sz(comps.back()) == 0);
while(sz(comps[sz(comps) - 1]) > 0){
swap(max_pos[centroid], max_pos[comps[sz(comps) - 1].back()]);
comps[sz(comps) - 1].pop_back();
}
cout << minAns << ' ' << 2 * maxAns << '\n';
for(int i = 1; i <= n; i++){
cout << min_pos[i] << ' ';
}
cout << '\n';
for(int i = 1; i <= n; i++){
cout << max_pos[i] << ' ';
}
cout << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
//cin >> T;
T = 1;
while(T--){
solve();
}
return 0;
}
/*
4
1 2
2 3
3 4
7
4 2
5 7
3 4
6 3
1 3
4 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
3 |
Correct |
1 ms |
3676 KB |
Output is correct |
4 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
5 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
6 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
7 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
8 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
9 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
1 ms |
3676 KB |
Output is correct |
12 |
Correct |
1 ms |
3676 KB |
Output is correct |
13 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
14 |
Correct |
1 ms |
3676 KB |
Output is correct |
15 |
Correct |
1 ms |
3676 KB |
Output is correct |
16 |
Correct |
1 ms |
3676 KB |
Output is correct |
17 |
Correct |
1 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
2 |
Partially correct |
1 ms |
3928 KB |
Partially correct |
3 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
4 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
5 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
6 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
7 |
Correct |
2 ms |
3676 KB |
Output is correct |
8 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
9 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
12 |
Correct |
2 ms |
3672 KB |
Output is correct |
13 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
14 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
15 |
Partially correct |
1 ms |
3928 KB |
Partially correct |
16 |
Partially correct |
0 ms |
3828 KB |
Partially correct |
17 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
18 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
19 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
20 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
21 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
22 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
23 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3676 KB |
Output is correct |
26 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
27 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
28 |
Partially correct |
2 ms |
3872 KB |
Partially correct |
29 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
30 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
3 |
Correct |
1 ms |
3676 KB |
Output is correct |
4 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
5 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
6 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
7 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
8 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
9 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
1 ms |
3676 KB |
Output is correct |
12 |
Correct |
1 ms |
3676 KB |
Output is correct |
13 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
14 |
Correct |
1 ms |
3676 KB |
Output is correct |
15 |
Correct |
1 ms |
3676 KB |
Output is correct |
16 |
Correct |
1 ms |
3676 KB |
Output is correct |
17 |
Correct |
1 ms |
3676 KB |
Output is correct |
18 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
19 |
Partially correct |
1 ms |
3928 KB |
Partially correct |
20 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
21 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
22 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
23 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
26 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
27 |
Correct |
1 ms |
3676 KB |
Output is correct |
28 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
29 |
Correct |
2 ms |
3672 KB |
Output is correct |
30 |
Partially correct |
1 ms |
3672 KB |
Partially correct |
31 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
32 |
Partially correct |
1 ms |
3928 KB |
Partially correct |
33 |
Partially correct |
0 ms |
3828 KB |
Partially correct |
34 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
35 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
36 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
37 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
38 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
39 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
40 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
41 |
Correct |
1 ms |
3676 KB |
Output is correct |
42 |
Correct |
2 ms |
3676 KB |
Output is correct |
43 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
44 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
45 |
Partially correct |
2 ms |
3872 KB |
Partially correct |
46 |
Partially correct |
1 ms |
3676 KB |
Partially correct |
47 |
Partially correct |
2 ms |
3676 KB |
Partially correct |
48 |
Partially correct |
43 ms |
8512 KB |
Partially correct |
49 |
Partially correct |
45 ms |
8916 KB |
Partially correct |
50 |
Partially correct |
42 ms |
9100 KB |
Partially correct |
51 |
Partially correct |
31 ms |
7760 KB |
Partially correct |
52 |
Partially correct |
40 ms |
8788 KB |
Partially correct |
53 |
Partially correct |
36 ms |
8528 KB |
Partially correct |
54 |
Correct |
24 ms |
9816 KB |
Output is correct |
55 |
Partially correct |
54 ms |
16332 KB |
Partially correct |
56 |
Partially correct |
52 ms |
12496 KB |
Partially correct |
57 |
Partially correct |
47 ms |
11216 KB |
Partially correct |
58 |
Correct |
52 ms |
10060 KB |
Output is correct |
59 |
Partially correct |
41 ms |
9168 KB |
Partially correct |
60 |
Correct |
37 ms |
14408 KB |
Output is correct |
61 |
Partially correct |
34 ms |
10080 KB |
Partially correct |
62 |
Partially correct |
43 ms |
9436 KB |
Partially correct |
63 |
Partially correct |
32 ms |
9004 KB |
Partially correct |
64 |
Partially correct |
36 ms |
9044 KB |
Partially correct |
65 |
Partially correct |
44 ms |
9480 KB |
Partially correct |
66 |
Partially correct |
35 ms |
9052 KB |
Partially correct |
67 |
Partially correct |
25 ms |
8332 KB |
Partially correct |
68 |
Partially correct |
33 ms |
8280 KB |
Partially correct |
69 |
Partially correct |
34 ms |
9424 KB |
Partially correct |
70 |
Partially correct |
34 ms |
9044 KB |
Partially correct |
71 |
Partially correct |
26 ms |
7772 KB |
Partially correct |
72 |
Partially correct |
28 ms |
8020 KB |
Partially correct |
73 |
Partially correct |
33 ms |
9380 KB |
Partially correct |
74 |
Partially correct |
31 ms |
8788 KB |
Partially correct |
75 |
Partially correct |
41 ms |
8656 KB |
Partially correct |
76 |
Partially correct |
41 ms |
8592 KB |
Partially correct |
77 |
Partially correct |
34 ms |
9048 KB |
Partially correct |
78 |
Partially correct |
29 ms |
7252 KB |
Partially correct |
79 |
Partially correct |
29 ms |
7764 KB |
Partially correct |
80 |
Partially correct |
43 ms |
8652 KB |
Partially correct |
81 |
Partially correct |
51 ms |
9056 KB |
Partially correct |
82 |
Partially correct |
36 ms |
9044 KB |
Partially correct |