# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
657337 |
2022-11-09T15:07:58 Z |
esomer |
Village (BOI20_village) |
C++17 |
|
199 ms |
41276 KB |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
const int MOD = 998244353;
struct LCA{
vector<vector<int>> dp;
vector<int> depths;
void DFS(int x, vector<vector<int>>& adj, int p){
dp[x][0] = p;
for(auto node : adj[x]){
if(node != p){
depths[node] = depths[x] + 1;
DFS(node, adj, x);
}
}
}
void build(vector<vector<int>>& adj){
int n = adj.size();
dp.assign(n, vector<int>(20, 0)); depths.resize(n);
depths[0] = 0;
DFS(0, adj, -1);
for(int i = 1; i < 20; i++){
for(int j = 0; j < n; j++){
if(dp[j][i - 1] == -1) {dp[j][i] = -1; continue;}
dp[j][i] = dp[dp[j][i - 1]][i - 1];
}
}
}
int kth(int x, int d){
for(int k = 19; k >= 0; k--){
if((1 << k) <= d){
x = dp[x][k];
d -= (1 << k);
}
}
return x;
}
int lca(int a, int b){
if(depths[b] > depths[a]) b = kth(b, depths[b] - depths[a]);
else if(depths[a] > depths[b]) a = kth(a, depths[a] - depths[b]);
if(a == b) return a;
int anc = -1;
for(int k = 19; k >= 0; k--){
if(dp[a][k] == dp[b][k]){
anc = max(0, dp[a][k]);
}else{
a = dp[a][k];
b = dp[b][k];
}
}
return anc;
}
int dist(int a, int b){
return depths[a] + depths[b] - 2 * depths[lca(a, b)];
}
};
struct maximum{
int centr = -1;
int find_centroid(int x, vector<vector<int>>& adj, int p){
if(centr != -1) return -1;
int n = adj.size();
int subtree = 1;
bool good = 1;
for(int node : adj[x]){
if(node == p) continue;
int sub = find_centroid(node, adj, x);
if(centr != -1) return -1;
subtree += sub;
if(sub > n / 2) good = 0;
}
if(good == 1 && n - subtree <= n / 2){
centr = x;
return -1;
}else return subtree;
}
void DFS(int x, vector<vector<int>>& adj, vector<int>& euler, int p){
euler.push_back(x);
for(int node : adj[x]){
if(node == p) continue;
DFS(node, adj, euler, x);
}
}
pair<ll, vector<int>> solve(vector<vector<int>> adj){
int n = adj.size();
find_centroid(0, adj, -1);
vector<int> euler;
DFS(centr, adj, euler, -1);
ll ans = 0;
LCA lca; lca.build(adj);
vector<int> v(n);
for(int i = 0; i < n; i++){
ans += lca.dist(euler[i], euler[(i + n / 2) % n]);
v[euler[i]] = euler[(i + n / 2) % n];
}
return {ans, v};
}
};
struct minimum{
vector<int> v;
int ans = 0;
bool DFS(int x, vector<vector<int>>& adj, int p){
vector<int> odd;
for(int node : adj[x]){
if(node == p) continue;
bool rep = DFS(node, adj, x);
if(rep) odd.push_back(node);
}
if(odd.size() == 0){
if(x == 0){
int node = adj[x][0];
if(v[v[node]] == node){
v[x] = v[node];
v[node] = x;
ans += 2;
}else{
int node1 = v[node];
int node2 = v[v[node]];
v[node1] = node2;
v[node2] = node1;
v[x] = node;
v[node] = x;
ans += 2;
}
}else return 1;
}else{
if(((int)odd.size()) % 2 == 1){
for(int i = 1; i < (int)odd.size() - 1; i += 2){
v[odd[i]] = odd[i + 1];
v[odd[i + 1]] = odd[i];
ans += 4;
}
v[x] = odd[0];
v[odd[0]] = x;
ans += 2;
}else{
for(int i = 2; i < (int)odd.size() - 1; i += 2){
v[odd[i]] = odd[i + 1];
v[odd[i + 1]] = odd[i];
ans += 4;
}
v[x] = odd[0];
v[odd[0]] = odd[1];
v[odd[1]] = x;
ans += 4;
}
}
return 0;
}
pair<int, vector<int>> solve(vector<vector<int>> adj){
int n = adj.size();
v.resize(n);
DFS(0, adj, -1);
return {ans, v};
}
};
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("inpt.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
//int tt; cin >> tt;
int n; cin >> n;
vector<vector<int>> adj(n);
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b; a--; b--;
adj[a].push_back(b); adj[b].push_back(a);
}
minimum men;
maximum mex;
pair<int, vector<int>> mn = men.solve(adj);
pair<ll, vector<int>> mx = mex.solve(adj);
cout << mn.first << " "<< mx.first << endl;
for(int x : mn.second) cout << x + 1 << " ";
cout << endl;
for(int x : mx.second) cout << x + 1 << " ";
cout << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
576 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
580 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
452 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
448 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
576 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
580 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
592 KB |
Output is correct |
37 |
Correct |
1 ms |
592 KB |
Output is correct |
38 |
Correct |
1 ms |
592 KB |
Output is correct |
39 |
Correct |
1 ms |
592 KB |
Output is correct |
40 |
Correct |
1 ms |
452 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
448 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
588 KB |
Output is correct |
47 |
Correct |
1 ms |
580 KB |
Output is correct |
48 |
Correct |
125 ms |
25284 KB |
Output is correct |
49 |
Correct |
157 ms |
27884 KB |
Output is correct |
50 |
Correct |
145 ms |
27880 KB |
Output is correct |
51 |
Correct |
106 ms |
21668 KB |
Output is correct |
52 |
Correct |
151 ms |
27564 KB |
Output is correct |
53 |
Correct |
140 ms |
24760 KB |
Output is correct |
54 |
Correct |
71 ms |
20280 KB |
Output is correct |
55 |
Correct |
199 ms |
41276 KB |
Output is correct |
56 |
Correct |
196 ms |
34084 KB |
Output is correct |
57 |
Correct |
173 ms |
31652 KB |
Output is correct |
58 |
Correct |
168 ms |
30028 KB |
Output is correct |
59 |
Correct |
165 ms |
27968 KB |
Output is correct |
60 |
Correct |
121 ms |
28320 KB |
Output is correct |
61 |
Correct |
135 ms |
28508 KB |
Output is correct |
62 |
Correct |
138 ms |
28592 KB |
Output is correct |
63 |
Correct |
114 ms |
26852 KB |
Output is correct |
64 |
Correct |
134 ms |
28448 KB |
Output is correct |
65 |
Correct |
136 ms |
28656 KB |
Output is correct |
66 |
Correct |
115 ms |
26792 KB |
Output is correct |
67 |
Correct |
81 ms |
21048 KB |
Output is correct |
68 |
Correct |
101 ms |
24160 KB |
Output is correct |
69 |
Correct |
126 ms |
28696 KB |
Output is correct |
70 |
Correct |
121 ms |
26936 KB |
Output is correct |
71 |
Correct |
77 ms |
20040 KB |
Output is correct |
72 |
Correct |
91 ms |
22576 KB |
Output is correct |
73 |
Correct |
126 ms |
28584 KB |
Output is correct |
74 |
Correct |
110 ms |
26172 KB |
Output is correct |
75 |
Correct |
144 ms |
27644 KB |
Output is correct |
76 |
Correct |
146 ms |
27680 KB |
Output is correct |
77 |
Correct |
133 ms |
26788 KB |
Output is correct |
78 |
Correct |
80 ms |
18828 KB |
Output is correct |
79 |
Correct |
91 ms |
21944 KB |
Output is correct |
80 |
Correct |
164 ms |
27672 KB |
Output is correct |
81 |
Correct |
139 ms |
26948 KB |
Output is correct |
82 |
Correct |
131 ms |
28240 KB |
Output is correct |