#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e5 + 11;
const int INF = (1LL << 60);
bool vis[MAXN];
vector<int> G[MAXN];
int c_max[MAXN], c_min[MAXN];
template<class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
int d[51][51];
int n;
void dijkstra(int s, int ans[]){
fill(ans, ans + n, INF);
fill(vis, vis + n, false);
ans[s] = 0;
min_priority_queue<pair<int, int>> pq; pq.push({0, s});
while(!pq.empty()){
auto [dis, v] = pq.top(); pq.pop();
if(vis[v]) continue; vis[v] = true;
for(auto u : G[v]){
if(ans[u] > ans[v] + 1){
ans[u] = ans[v] + 1;
pq.push({ans[u], u});
}
}
}
}
void naive_dist(){
for(int i = 0; i < n; i++){
dijkstra(i, d[i]);
}
}
int naive_min(){
int p[10]; for(int i = 0; i < n; i++) p[i] = i;
int min_dist = INF;
do{
bool fail = false;
for(int i = 0; i < n; i++) if(p[i] == i) fail = true;
if(fail) continue;
int dist = 0; for(int i = 0; i < n; i++) dist += d[i][p[i]];
min_dist = min(min_dist, dist);
}while(next_permutation(p, p + n));
return min_dist;
}
int naive_max(){
int p[10]; for(int i = 0; i < n; i++) p[i] = i;
int max_dist = -INF;
do{
bool fail = false;
for(int i = 0; i < n; i++) if(p[i] == i) fail = true;
if(fail) continue;
int dist = 0; for(int i = 0; i < n; i++) dist += d[i][p[i]];
max_dist = max(max_dist, dist);
}while(next_permutation(p, p + n));
return max_dist;
}
int counter = 0;
void dfs_min(int v, int pa = -1){
for(auto u : G[v]){
if(u == pa) continue;
dfs_min(u, v);
if(c_min[u] == u) swap(c_min[u], c_min[v]), counter += 2;
}
if(pa == -1 && c_min[v] == v){
for(auto u : G[v]){
if(u == pa) continue;
swap(c_min[u], c_min[v]), counter += 2;
break;
}
}
}
int clever_min(){
for(int i = 0; i < n; i++) c_min[i] = i;
counter = 0;
dfs_min(0);
return counter;
}
int sz[MAXN], p[MAXN];
void dfs_max(int v, int pa = -1){
sz[v] = 1;
for(auto u : G[v]){
if(u == pa) continue;
dfs_max(u, v); p[u] = v; sz[v] += sz[u];
}
}
int centroid(int v, int pa = -1){
for(auto u : G[v]){
if(u == pa) continue;
if(sz[u] >= (n + 1) / 2) return centroid(u, v);
}
return v;
}
vector<int> ST[MAXN];
void dfs_st(int s, int v, int pa = -1){
for(auto u : G[v]){
if(u == pa) continue;
dfs_st(s, u, v);
}
ST[s].push_back(v);
}
int clever_max(){
dfs_max(0);
int c = centroid(0);
dfs_max(c);
int s = 0;
for(int i = 0; i < n; i++){
s += min(sz[i], n - sz[i]) * 2;
}
priority_queue<pair<int, int>> pq;
for(auto v : G[c]){
dfs_st(v, v, c); pq.push({sz[v], v});
}
int prv = -1;
vector<int> ord {c};
while(!pq.empty()){
auto [s, v] = pq.top(); pq.pop();
int u = ST[v][sz[v] - 1]; sz[v]--;
ord.push_back(u);
if(prv != -1 && sz[prv] > 0) pq.push({sz[prv], prv});
prv = v;
}
for(int i = 0; i < n; i++){
c_max[ord[i]] = ord[(i + 1) % n];
}
for(int i = 0; i < n; i++){
if(c_max[i] == i){
swap(c_max[i], c_max[G[i][0]]);
}
}
return s;
}
int32_t main(){
cin >> n;
for(int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v; u--; v--;
G[u].push_back(v); G[v].push_back(u);
}
int d2_min = clever_min(), d2_max = clever_max();
cout << d2_min << ' ' << d2_max << endl;
for(int i = 0; i < n; i++) cout << c_min[i] + 1 << " \n"[i == n - 1];
for(int i = 0; i < n; i++) cout << c_max[i] + 1 << " \n"[i == n - 1];
}
Compilation message
Village.cpp: In function 'void dijkstra(long long int, long long int*)':
Village.cpp:22:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | auto [dis, v] = pq.top(); pq.pop();
| ^
Village.cpp:23:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
23 | if(vis[v]) continue; vis[v] = true;
| ^~
Village.cpp:23:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
23 | if(vis[v]) continue; vis[v] = true;
| ^~~
Village.cpp: In function 'long long int clever_max()':
Village.cpp:131:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
131 | auto [s, v] = pq.top(); pq.pop();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5016 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5088 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
4948 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
2 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
5016 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5076 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5088 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
4948 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
2 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5076 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
3 ms |
5076 KB |
Output is correct |
48 |
Correct |
79 ms |
13200 KB |
Output is correct |
49 |
Correct |
91 ms |
14512 KB |
Output is correct |
50 |
Correct |
78 ms |
14108 KB |
Output is correct |
51 |
Correct |
64 ms |
12020 KB |
Output is correct |
52 |
Correct |
78 ms |
14084 KB |
Output is correct |
53 |
Correct |
72 ms |
13000 KB |
Output is correct |
54 |
Correct |
43 ms |
12692 KB |
Output is correct |
55 |
Correct |
136 ms |
20936 KB |
Output is correct |
56 |
Correct |
89 ms |
17448 KB |
Output is correct |
57 |
Correct |
92 ms |
16168 KB |
Output is correct |
58 |
Correct |
84 ms |
15048 KB |
Output is correct |
59 |
Correct |
91 ms |
14064 KB |
Output is correct |
60 |
Correct |
84 ms |
18480 KB |
Output is correct |
61 |
Correct |
82 ms |
15040 KB |
Output is correct |
62 |
Correct |
75 ms |
14332 KB |
Output is correct |
63 |
Correct |
85 ms |
13856 KB |
Output is correct |
64 |
Correct |
72 ms |
14464 KB |
Output is correct |
65 |
Correct |
77 ms |
14668 KB |
Output is correct |
66 |
Correct |
87 ms |
13916 KB |
Output is correct |
67 |
Correct |
56 ms |
12196 KB |
Output is correct |
68 |
Correct |
78 ms |
13188 KB |
Output is correct |
69 |
Correct |
77 ms |
15036 KB |
Output is correct |
70 |
Correct |
69 ms |
14172 KB |
Output is correct |
71 |
Correct |
49 ms |
11804 KB |
Output is correct |
72 |
Correct |
56 ms |
12652 KB |
Output is correct |
73 |
Correct |
73 ms |
14656 KB |
Output is correct |
74 |
Correct |
76 ms |
13912 KB |
Output is correct |
75 |
Correct |
83 ms |
14408 KB |
Output is correct |
76 |
Correct |
77 ms |
14356 KB |
Output is correct |
77 |
Correct |
79 ms |
14276 KB |
Output is correct |
78 |
Correct |
49 ms |
11356 KB |
Output is correct |
79 |
Correct |
56 ms |
12120 KB |
Output is correct |
80 |
Correct |
81 ms |
14780 KB |
Output is correct |
81 |
Correct |
73 ms |
14420 KB |
Output is correct |
82 |
Correct |
70 ms |
14084 KB |
Output is correct |