#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){
for(int vec : adj[node]){
if(vec != par){
dfs3(vec, node);
}
}
aux.pb(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;
}
dfs3(centroid, 0);
for(int i = 0; i < n / 2; i++){
swap(max_pos[aux[i]], max_pos[aux[n / 2 + i]]);
}
if(max_pos[centroid] == centroid){
max_pos[centroid] = adj[centroid][0];
max_pos[max_pos[adj[centroid][0]]] = centroid;
//swap(max_pos[centroid], max_pos[adj[centroid][0]]);
}
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
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
3676 KB |
Output is correct |
5 |
Correct |
1 ms |
3676 KB |
Output is correct |
6 |
Correct |
1 ms |
3676 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
3676 KB |
Output is correct |
9 |
Correct |
1 ms |
3676 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
1 ms |
3764 KB |
Output is correct |
12 |
Correct |
1 ms |
3768 KB |
Output is correct |
13 |
Correct |
1 ms |
3676 KB |
Output is 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 |
2 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3672 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Correct |
2 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
3676 KB |
Output is correct |
5 |
Correct |
1 ms |
3676 KB |
Output is correct |
6 |
Correct |
1 ms |
3676 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
3676 KB |
Output is correct |
9 |
Correct |
1 ms |
3676 KB |
Output is correct |
10 |
Correct |
1 ms |
3672 KB |
Output is correct |
11 |
Correct |
1 ms |
3676 KB |
Output is correct |
12 |
Correct |
1 ms |
3676 KB |
Output is correct |
13 |
Correct |
1 ms |
3676 KB |
Output is correct |
14 |
Correct |
2 ms |
3680 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 |
Correct |
1 ms |
3676 KB |
Output is correct |
19 |
Correct |
1 ms |
3804 KB |
Output is correct |
20 |
Correct |
1 ms |
3672 KB |
Output is correct |
21 |
Correct |
1 ms |
3672 KB |
Output is correct |
22 |
Correct |
1 ms |
3676 KB |
Output is correct |
23 |
Correct |
2 ms |
3676 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
1 ms |
3676 KB |
Output is correct |
26 |
Correct |
1 ms |
3836 KB |
Output is correct |
27 |
Correct |
1 ms |
3676 KB |
Output is correct |
28 |
Correct |
2 ms |
3672 KB |
Output is correct |
29 |
Correct |
2 ms |
3672 KB |
Output is correct |
30 |
Correct |
1 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3676 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Correct |
1 ms |
3676 KB |
Output is correct |
4 |
Correct |
1 ms |
3676 KB |
Output is correct |
5 |
Correct |
1 ms |
3676 KB |
Output is correct |
6 |
Correct |
1 ms |
3676 KB |
Output is correct |
7 |
Correct |
1 ms |
3676 KB |
Output is correct |
8 |
Correct |
1 ms |
3676 KB |
Output is correct |
9 |
Correct |
1 ms |
3676 KB |
Output is correct |
10 |
Correct |
1 ms |
3676 KB |
Output is correct |
11 |
Correct |
1 ms |
3764 KB |
Output is correct |
12 |
Correct |
1 ms |
3768 KB |
Output is correct |
13 |
Correct |
1 ms |
3676 KB |
Output is 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 |
2 ms |
3676 KB |
Output is correct |
18 |
Correct |
1 ms |
3672 KB |
Output is correct |
19 |
Correct |
1 ms |
3676 KB |
Output is correct |
20 |
Correct |
2 ms |
3676 KB |
Output is correct |
21 |
Correct |
1 ms |
3676 KB |
Output is correct |
22 |
Correct |
1 ms |
3676 KB |
Output is correct |
23 |
Correct |
1 ms |
3676 KB |
Output is correct |
24 |
Correct |
1 ms |
3676 KB |
Output is correct |
25 |
Correct |
1 ms |
3676 KB |
Output is correct |
26 |
Correct |
1 ms |
3676 KB |
Output is correct |
27 |
Correct |
1 ms |
3672 KB |
Output is correct |
28 |
Correct |
1 ms |
3676 KB |
Output is correct |
29 |
Correct |
1 ms |
3676 KB |
Output is correct |
30 |
Correct |
1 ms |
3676 KB |
Output is correct |
31 |
Correct |
2 ms |
3680 KB |
Output is correct |
32 |
Correct |
1 ms |
3676 KB |
Output is correct |
33 |
Correct |
1 ms |
3676 KB |
Output is correct |
34 |
Correct |
1 ms |
3676 KB |
Output is correct |
35 |
Correct |
1 ms |
3676 KB |
Output is correct |
36 |
Correct |
1 ms |
3804 KB |
Output is correct |
37 |
Correct |
1 ms |
3672 KB |
Output is correct |
38 |
Correct |
1 ms |
3672 KB |
Output is correct |
39 |
Correct |
1 ms |
3676 KB |
Output is correct |
40 |
Correct |
2 ms |
3676 KB |
Output is correct |
41 |
Correct |
1 ms |
3676 KB |
Output is correct |
42 |
Correct |
1 ms |
3676 KB |
Output is correct |
43 |
Correct |
1 ms |
3836 KB |
Output is correct |
44 |
Correct |
1 ms |
3676 KB |
Output is correct |
45 |
Correct |
2 ms |
3672 KB |
Output is correct |
46 |
Correct |
2 ms |
3672 KB |
Output is correct |
47 |
Correct |
1 ms |
3676 KB |
Output is correct |
48 |
Correct |
39 ms |
8392 KB |
Output is correct |
49 |
Correct |
40 ms |
8912 KB |
Output is correct |
50 |
Correct |
41 ms |
8916 KB |
Output is correct |
51 |
Correct |
32 ms |
7628 KB |
Output is correct |
52 |
Correct |
40 ms |
8824 KB |
Output is correct |
53 |
Correct |
40 ms |
8400 KB |
Output is correct |
54 |
Correct |
24 ms |
9900 KB |
Output is correct |
55 |
Partially correct |
51 ms |
16332 KB |
Partially correct |
56 |
Partially correct |
49 ms |
12236 KB |
Partially correct |
57 |
Correct |
44 ms |
10952 KB |
Output is correct |
58 |
Correct |
47 ms |
10080 KB |
Output is correct |
59 |
Correct |
41 ms |
8908 KB |
Output is correct |
60 |
Correct |
35 ms |
9036 KB |
Output is correct |
61 |
Correct |
34 ms |
9168 KB |
Output is correct |
62 |
Correct |
34 ms |
9312 KB |
Output is correct |
63 |
Correct |
33 ms |
8920 KB |
Output is correct |
64 |
Correct |
47 ms |
9224 KB |
Output is correct |
65 |
Correct |
33 ms |
9336 KB |
Output is correct |
66 |
Correct |
31 ms |
8924 KB |
Output is correct |
67 |
Correct |
34 ms |
7892 KB |
Output is correct |
68 |
Correct |
29 ms |
8460 KB |
Output is correct |
69 |
Correct |
33 ms |
9328 KB |
Output is correct |
70 |
Correct |
34 ms |
8912 KB |
Output is correct |
71 |
Correct |
24 ms |
7880 KB |
Output is correct |
72 |
Correct |
26 ms |
8140 KB |
Output is correct |
73 |
Correct |
42 ms |
9672 KB |
Output is correct |
74 |
Correct |
31 ms |
8916 KB |
Output is correct |
75 |
Correct |
41 ms |
8632 KB |
Output is correct |
76 |
Correct |
49 ms |
8648 KB |
Output is correct |
77 |
Correct |
33 ms |
8916 KB |
Output is correct |
78 |
Correct |
26 ms |
7372 KB |
Output is correct |
79 |
Correct |
29 ms |
7800 KB |
Output is correct |
80 |
Correct |
39 ms |
8652 KB |
Output is correct |
81 |
Correct |
35 ms |
9140 KB |
Output is correct |
82 |
Correct |
52 ms |
9160 KB |
Output is correct |