#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int mod = 1e9 + 7;
const int N = 2e5;
int dp[N], dp1[N], in[N], sub[N];
int n, ans, ans1;
vector<int> g[N];
int timer = 1;
void dfs(int v, int par){
in[v] = timer++;
sub[v] = 1;
for(int to : g[v]){
if(to != par){
dfs(to, v);
sub[v]+= sub[to];
}
}
ans1+= 2 * min(sub[v], n - sub[v]);
if(par != v && dp[v] == -1 && dp[par] == -1){
dp[v] = par, dp[par] = v;
ans+= 2;
}
}
int centroid(int v, int par){
for(int to : g[v]){
if(to == par) continue;
if(2 * sub[to] > n){
return centroid(to, v);
}
}
return v;
}
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for(int i = 0;i < N; i++){
dp[i] = -1;
dp1[i] = i;
}
cin >> n;
for(int i = 0;i < n-1; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int centre = centroid(1, 1);
dfs(centre, centre);
for(int i = 1;i <= n; i++){
if(dp[i] == -1){
int to = g[i][0];
dp[i] = i;
swap(dp[to], dp[i]);
ans+= 2;
}
}
vector<int> ord;
vector<pair<int, int> > vec;
for(int i = 1;i <= n; i++) vec.push_back({in[i], i});
sort(all(vec));
for(int i = 0;i < n; i++){
ord.push_back(vec[i].ss);
}
for(int i = 0;i < n; i++){
dp1[ord[i]] = ord[(i + n / 2) % n];
}
cout << ans << ' ' << ans1 << '\n';
for(int i = 1;i <= n; i++) cout << dp[i] << ' ';
cout << '\n';
for(int i= 1;i <= n; i++) cout << dp1[i] << ' ';
return 0;
}
Compilation message
Village.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
42 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
3 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11508 KB |
Output is correct |
4 |
Correct |
2 ms |
11356 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
3 ms |
11356 KB |
Output is correct |
8 |
Correct |
2 ms |
11356 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11416 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
2 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11356 KB |
Output is correct |
4 |
Correct |
3 ms |
11356 KB |
Output is correct |
5 |
Correct |
2 ms |
11368 KB |
Output is correct |
6 |
Correct |
3 ms |
11356 KB |
Output is correct |
7 |
Correct |
2 ms |
11356 KB |
Output is correct |
8 |
Correct |
3 ms |
11356 KB |
Output is correct |
9 |
Correct |
2 ms |
11352 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
3 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
3 ms |
11356 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11352 KB |
Output is correct |
19 |
Correct |
2 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11356 KB |
Output is correct |
21 |
Correct |
2 ms |
11356 KB |
Output is correct |
22 |
Correct |
2 ms |
11352 KB |
Output is correct |
23 |
Correct |
2 ms |
11352 KB |
Output is correct |
24 |
Correct |
3 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11356 KB |
Output is correct |
27 |
Correct |
3 ms |
11352 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
29 |
Correct |
3 ms |
11356 KB |
Output is correct |
30 |
Correct |
2 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
11356 KB |
Output is correct |
2 |
Correct |
3 ms |
11356 KB |
Output is correct |
3 |
Correct |
2 ms |
11508 KB |
Output is correct |
4 |
Correct |
2 ms |
11356 KB |
Output is correct |
5 |
Correct |
3 ms |
11356 KB |
Output is correct |
6 |
Correct |
2 ms |
11356 KB |
Output is correct |
7 |
Correct |
3 ms |
11356 KB |
Output is correct |
8 |
Correct |
2 ms |
11356 KB |
Output is correct |
9 |
Correct |
2 ms |
11356 KB |
Output is correct |
10 |
Correct |
2 ms |
11356 KB |
Output is correct |
11 |
Correct |
2 ms |
11356 KB |
Output is correct |
12 |
Correct |
2 ms |
11356 KB |
Output is correct |
13 |
Correct |
2 ms |
11356 KB |
Output is correct |
14 |
Correct |
2 ms |
11356 KB |
Output is correct |
15 |
Correct |
2 ms |
11416 KB |
Output is correct |
16 |
Correct |
2 ms |
11356 KB |
Output is correct |
17 |
Correct |
2 ms |
11356 KB |
Output is correct |
18 |
Correct |
2 ms |
11356 KB |
Output is correct |
19 |
Correct |
2 ms |
11356 KB |
Output is correct |
20 |
Correct |
2 ms |
11356 KB |
Output is correct |
21 |
Correct |
3 ms |
11356 KB |
Output is correct |
22 |
Correct |
2 ms |
11368 KB |
Output is correct |
23 |
Correct |
3 ms |
11356 KB |
Output is correct |
24 |
Correct |
2 ms |
11356 KB |
Output is correct |
25 |
Correct |
3 ms |
11356 KB |
Output is correct |
26 |
Correct |
2 ms |
11352 KB |
Output is correct |
27 |
Correct |
2 ms |
11356 KB |
Output is correct |
28 |
Correct |
2 ms |
11356 KB |
Output is correct |
29 |
Correct |
2 ms |
11356 KB |
Output is correct |
30 |
Correct |
3 ms |
11356 KB |
Output is correct |
31 |
Correct |
2 ms |
11356 KB |
Output is correct |
32 |
Correct |
3 ms |
11356 KB |
Output is correct |
33 |
Correct |
2 ms |
11356 KB |
Output is correct |
34 |
Correct |
2 ms |
11356 KB |
Output is correct |
35 |
Correct |
2 ms |
11352 KB |
Output is correct |
36 |
Correct |
2 ms |
11356 KB |
Output is correct |
37 |
Correct |
2 ms |
11356 KB |
Output is correct |
38 |
Correct |
2 ms |
11356 KB |
Output is correct |
39 |
Correct |
2 ms |
11352 KB |
Output is correct |
40 |
Correct |
2 ms |
11352 KB |
Output is correct |
41 |
Correct |
3 ms |
11356 KB |
Output is correct |
42 |
Correct |
2 ms |
11356 KB |
Output is correct |
43 |
Correct |
2 ms |
11356 KB |
Output is correct |
44 |
Correct |
3 ms |
11352 KB |
Output is correct |
45 |
Correct |
2 ms |
11356 KB |
Output is correct |
46 |
Correct |
3 ms |
11356 KB |
Output is correct |
47 |
Correct |
2 ms |
11356 KB |
Output is correct |
48 |
Correct |
44 ms |
18988 KB |
Output is correct |
49 |
Correct |
54 ms |
19632 KB |
Output is correct |
50 |
Correct |
48 ms |
19648 KB |
Output is correct |
51 |
Correct |
42 ms |
18116 KB |
Output is correct |
52 |
Correct |
57 ms |
19644 KB |
Output is correct |
53 |
Correct |
42 ms |
18748 KB |
Output is correct |
54 |
Correct |
30 ms |
17876 KB |
Output is correct |
55 |
Correct |
52 ms |
25024 KB |
Output is correct |
56 |
Correct |
58 ms |
22468 KB |
Output is correct |
57 |
Correct |
52 ms |
21464 KB |
Output is correct |
58 |
Correct |
49 ms |
20420 KB |
Output is correct |
59 |
Correct |
47 ms |
19652 KB |
Output is correct |
60 |
Correct |
49 ms |
19668 KB |
Output is correct |
61 |
Correct |
49 ms |
19816 KB |
Output is correct |
62 |
Correct |
44 ms |
19940 KB |
Output is correct |
63 |
Correct |
45 ms |
19400 KB |
Output is correct |
64 |
Correct |
45 ms |
19908 KB |
Output is correct |
65 |
Correct |
42 ms |
20220 KB |
Output is correct |
66 |
Correct |
42 ms |
19396 KB |
Output is correct |
67 |
Correct |
41 ms |
17856 KB |
Output is correct |
68 |
Correct |
40 ms |
18804 KB |
Output is correct |
69 |
Correct |
53 ms |
20164 KB |
Output is correct |
70 |
Correct |
48 ms |
19876 KB |
Output is correct |
71 |
Correct |
30 ms |
17868 KB |
Output is correct |
72 |
Correct |
35 ms |
18368 KB |
Output is correct |
73 |
Correct |
46 ms |
20736 KB |
Output is correct |
74 |
Correct |
39 ms |
19460 KB |
Output is correct |
75 |
Correct |
49 ms |
20164 KB |
Output is correct |
76 |
Correct |
55 ms |
19484 KB |
Output is correct |
77 |
Correct |
48 ms |
19904 KB |
Output is correct |
78 |
Correct |
32 ms |
17348 KB |
Output is correct |
79 |
Correct |
36 ms |
18888 KB |
Output is correct |
80 |
Correct |
48 ms |
20064 KB |
Output is correct |
81 |
Correct |
59 ms |
20172 KB |
Output is correct |
82 |
Correct |
43 ms |
19916 KB |
Output is correct |