#include <bits/stdc++.h>
typedef long long ll;
constexpr int M = 1e6+7;
using namespace std;
vector<vector<int>> g(M);
int paramin[M];
int paramax[M];
int par[M];
int sajz[M];
int kolor[M];
int odl[M];
vector<int> preorder;
vector<int> kolory[M];
void bfs(){
queue<int> q;
q.push(1);
preorder.push_back(1);
while(!q.empty()){
int p = q.front();
q.pop();
for(auto i:g[p]) if(par[i] == 0 && par[p]!=i){
q.push(i);
par[i] = p;
preorder.push_back(i);
}
}
}
void bfs2(int v){
queue<int> q;
q.push(v);
while(!q.empty()){
int p = q.front();
q.pop();
for(auto i:g[p]) if(i != v && odl[i]==0){
odl[i] = odl[p] + 1;
q.push(i);
}
}
}
int centroid = 0;
void dfs(int v){
pair<int, int> maks = {0, 0};
for(auto i:g[v]) if(i!=par[v]){
if(sajz[i] > maks.first) maks = {sajz[i], i};
}
if(maks.first > sajz[1]/2) dfs(maks.second);
else centroid = v;
}
void dfs2(int v, int k){
kolor[v] = k;
for(auto i:g[v]) if(i!=centroid && kolor[i]==0) dfs2(i, k);
}
bool czy(vector<int> a, vector<int> b) { return a.size() > b.size(); }
int main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin>>n;
for(int i=1; i<n; i++){
int a, b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
bfs();
reverse(preorder.begin(), preorder.end());
int minwynik = 0;
for(auto i:preorder){
sajz[i] = 1;
for(auto j:g[i]) if(j!=par[i]) sajz[i] += sajz[j];
int last = 0;
int p1 = 0, p2 = 0;
for(auto j:g[i]) if(j!=par[i] && paramin[j]==0){
if(last==0) last = j;
else{
p1 = last, p2 = j;
minwynik += 4;
paramin[last] = j;
paramin[j] = last;
last = 0;
}
}
if(last!=0){
minwynik += 2;
paramin[i] = last;
paramin[last] = i;
}
else{
paramin[p1] = i;
paramin[p2] = p1;
paramin[i] = p2;
}
}
if(paramin[1] == 0){
int a = g[1][0], b = paramin[a];
paramin[a] = 1;
paramin[b] = a;
paramin[1] = b;
minwynik+=2;
}
dfs(1);
int aktk = 0;
for(auto i:g[centroid]) dfs2(i, ++aktk);
for(int i=1; i<=n; i++) if(i!=centroid) kolory[kolor[i]].push_back(i);
priority_queue<pair<int, int>> q;
for(int i=1; i<=aktk; i++){
//cout<<kolory[i].size()<<" "<<i<<endl;
q.push({kolory[i].size(), i});
}
bfs2(centroid);
//for(int i=1; i<=n; i++) cout<<i<<": "<<odl[i]<<endl;
int makswyn = 0;
while(q.size() > 1){
pair<int, int> a = q.top();
q.pop();
pair<int, int> b = q.top();
q.pop();
int aa = kolory[a.second].back();
kolory[a.second].pop_back();
int bb = kolory[b.second].back();
kolory[b.second].pop_back();
//cout<<"PARUJEMY: "<<aa<<" "<<bb<<endl;
makswyn += (odl[aa] + odl[bb])*2;
paramax[aa] = bb;
paramax[bb] = aa;
a.first--, b.first--;
if(a.first) q.push(a);
if(b.first) q.push(b);
}
if(!q.empty()){
pair<int, int> a = q.top();
q.pop();
int aa = kolory[a.second].back();
kolory[a.second].pop_back();
//cout<<"PARUJEMY: "<<centroid<<" "<<aa<<endl;
makswyn += odl[aa]*2;
paramax[centroid] = aa;
paramax[aa] = centroid;
}
if(paramax[centroid] == 0){
int x = 1;
if(centroid == 1) x = 2;
int a = paramax[x];
paramax[a] = centroid;
paramax[centroid] = x;
}
cout<<minwynik<<" "<<makswyn<<endl;
for(int i=1; i<=n; i++) cout<<paramin[i]<<" ";
cout<<endl;
for(int i=1; i<=n; i++) cout<<paramax[i]<<" ";
cout<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47448 KB |
Output is correct |
2 |
Correct |
24 ms |
47452 KB |
Output is correct |
3 |
Correct |
22 ms |
47452 KB |
Output is correct |
4 |
Correct |
21 ms |
47448 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
22 ms |
47208 KB |
Output is correct |
7 |
Correct |
21 ms |
47448 KB |
Output is correct |
8 |
Partially correct |
23 ms |
47452 KB |
Partially correct |
9 |
Correct |
21 ms |
47452 KB |
Output is correct |
10 |
Correct |
22 ms |
47452 KB |
Output is correct |
11 |
Correct |
22 ms |
47440 KB |
Output is correct |
12 |
Correct |
27 ms |
47452 KB |
Output is correct |
13 |
Correct |
22 ms |
47448 KB |
Output is correct |
14 |
Partially correct |
21 ms |
47448 KB |
Partially correct |
15 |
Correct |
21 ms |
47448 KB |
Output is correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
21 ms |
47320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
28 ms |
47452 KB |
Partially correct |
2 |
Correct |
21 ms |
47452 KB |
Output is correct |
3 |
Partially correct |
21 ms |
47448 KB |
Partially correct |
4 |
Correct |
22 ms |
47452 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
27 ms |
47428 KB |
Output is correct |
7 |
Correct |
22 ms |
47452 KB |
Output is correct |
8 |
Correct |
21 ms |
47452 KB |
Output is correct |
9 |
Correct |
21 ms |
47320 KB |
Output is correct |
10 |
Correct |
21 ms |
47448 KB |
Output is correct |
11 |
Correct |
21 ms |
47396 KB |
Output is correct |
12 |
Partially correct |
22 ms |
47452 KB |
Partially correct |
13 |
Correct |
21 ms |
47436 KB |
Output is correct |
14 |
Correct |
21 ms |
47448 KB |
Output is correct |
15 |
Partially correct |
22 ms |
47560 KB |
Partially correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
21 ms |
47452 KB |
Output is correct |
18 |
Partially correct |
22 ms |
47448 KB |
Partially correct |
19 |
Partially correct |
22 ms |
47452 KB |
Partially correct |
20 |
Correct |
21 ms |
47448 KB |
Output is correct |
21 |
Partially correct |
21 ms |
47452 KB |
Partially correct |
22 |
Partially correct |
22 ms |
47552 KB |
Partially correct |
23 |
Correct |
21 ms |
47448 KB |
Output is correct |
24 |
Correct |
21 ms |
47452 KB |
Output is correct |
25 |
Correct |
22 ms |
47452 KB |
Output is correct |
26 |
Correct |
21 ms |
47452 KB |
Output is correct |
27 |
Partially correct |
24 ms |
47452 KB |
Partially correct |
28 |
Correct |
22 ms |
47452 KB |
Output is correct |
29 |
Partially correct |
21 ms |
47544 KB |
Partially correct |
30 |
Correct |
22 ms |
47452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
47448 KB |
Output is correct |
2 |
Correct |
24 ms |
47452 KB |
Output is correct |
3 |
Correct |
22 ms |
47452 KB |
Output is correct |
4 |
Correct |
21 ms |
47448 KB |
Output is correct |
5 |
Correct |
21 ms |
47452 KB |
Output is correct |
6 |
Correct |
22 ms |
47208 KB |
Output is correct |
7 |
Correct |
21 ms |
47448 KB |
Output is correct |
8 |
Partially correct |
23 ms |
47452 KB |
Partially correct |
9 |
Correct |
21 ms |
47452 KB |
Output is correct |
10 |
Correct |
22 ms |
47452 KB |
Output is correct |
11 |
Correct |
22 ms |
47440 KB |
Output is correct |
12 |
Correct |
27 ms |
47452 KB |
Output is correct |
13 |
Correct |
22 ms |
47448 KB |
Output is correct |
14 |
Partially correct |
21 ms |
47448 KB |
Partially correct |
15 |
Correct |
21 ms |
47448 KB |
Output is correct |
16 |
Correct |
20 ms |
47452 KB |
Output is correct |
17 |
Correct |
21 ms |
47320 KB |
Output is correct |
18 |
Partially correct |
28 ms |
47452 KB |
Partially correct |
19 |
Correct |
21 ms |
47452 KB |
Output is correct |
20 |
Partially correct |
21 ms |
47448 KB |
Partially correct |
21 |
Correct |
22 ms |
47452 KB |
Output is correct |
22 |
Correct |
21 ms |
47452 KB |
Output is correct |
23 |
Correct |
27 ms |
47428 KB |
Output is correct |
24 |
Correct |
22 ms |
47452 KB |
Output is correct |
25 |
Correct |
21 ms |
47452 KB |
Output is correct |
26 |
Correct |
21 ms |
47320 KB |
Output is correct |
27 |
Correct |
21 ms |
47448 KB |
Output is correct |
28 |
Correct |
21 ms |
47396 KB |
Output is correct |
29 |
Partially correct |
22 ms |
47452 KB |
Partially correct |
30 |
Correct |
21 ms |
47436 KB |
Output is correct |
31 |
Correct |
21 ms |
47448 KB |
Output is correct |
32 |
Partially correct |
22 ms |
47560 KB |
Partially correct |
33 |
Correct |
20 ms |
47452 KB |
Output is correct |
34 |
Correct |
21 ms |
47452 KB |
Output is correct |
35 |
Partially correct |
22 ms |
47448 KB |
Partially correct |
36 |
Partially correct |
22 ms |
47452 KB |
Partially correct |
37 |
Correct |
21 ms |
47448 KB |
Output is correct |
38 |
Partially correct |
21 ms |
47452 KB |
Partially correct |
39 |
Partially correct |
22 ms |
47552 KB |
Partially correct |
40 |
Correct |
21 ms |
47448 KB |
Output is correct |
41 |
Correct |
21 ms |
47452 KB |
Output is correct |
42 |
Correct |
22 ms |
47452 KB |
Output is correct |
43 |
Correct |
21 ms |
47452 KB |
Output is correct |
44 |
Partially correct |
24 ms |
47452 KB |
Partially correct |
45 |
Correct |
22 ms |
47452 KB |
Output is correct |
46 |
Partially correct |
21 ms |
47544 KB |
Partially correct |
47 |
Correct |
22 ms |
47452 KB |
Output is correct |
48 |
Correct |
65 ms |
54476 KB |
Output is correct |
49 |
Partially correct |
66 ms |
55168 KB |
Partially correct |
50 |
Partially correct |
73 ms |
55148 KB |
Partially correct |
51 |
Correct |
57 ms |
53452 KB |
Output is correct |
52 |
Correct |
67 ms |
55244 KB |
Output is correct |
53 |
Partially correct |
61 ms |
54520 KB |
Partially correct |
54 |
Correct |
44 ms |
52432 KB |
Output is correct |
55 |
Partially correct |
74 ms |
57468 KB |
Partially correct |
56 |
Partially correct |
74 ms |
56380 KB |
Partially correct |
57 |
Correct |
71 ms |
55756 KB |
Output is correct |
58 |
Correct |
69 ms |
55640 KB |
Output is correct |
59 |
Correct |
82 ms |
55240 KB |
Output is correct |
60 |
Partially correct |
71 ms |
59064 KB |
Partially correct |
61 |
Partially correct |
61 ms |
56264 KB |
Partially correct |
62 |
Partially correct |
61 ms |
55756 KB |
Partially correct |
63 |
Partially correct |
58 ms |
55308 KB |
Partially correct |
64 |
Partially correct |
61 ms |
55424 KB |
Partially correct |
65 |
Partially correct |
61 ms |
55772 KB |
Partially correct |
66 |
Partially correct |
64 ms |
55076 KB |
Partially correct |
67 |
Correct |
52 ms |
53964 KB |
Output is correct |
68 |
Partially correct |
54 ms |
54220 KB |
Partially correct |
69 |
Correct |
65 ms |
55776 KB |
Output is correct |
70 |
Correct |
58 ms |
55124 KB |
Output is correct |
71 |
Correct |
48 ms |
53256 KB |
Output is correct |
72 |
Correct |
51 ms |
54060 KB |
Output is correct |
73 |
Correct |
70 ms |
55756 KB |
Output is correct |
74 |
Correct |
56 ms |
54988 KB |
Output is correct |
75 |
Partially correct |
67 ms |
54988 KB |
Partially correct |
76 |
Correct |
62 ms |
55236 KB |
Output is correct |
77 |
Correct |
62 ms |
55344 KB |
Output is correct |
78 |
Partially correct |
48 ms |
52764 KB |
Partially correct |
79 |
Correct |
52 ms |
53708 KB |
Output is correct |
80 |
Partially correct |
68 ms |
55040 KB |
Partially correct |
81 |
Correct |
60 ms |
55496 KB |
Output is correct |
82 |
Partially correct |
59 ms |
55504 KB |
Partially correct |