#include <bits/stdc++.h>
using namespace std;
#define int long long
#define speedIO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define ff first
#define ss second
#define pii pair<int,int>
#define pb push_back
vector<vector<int>> graph;
vector<int> sz,order,depth,ansmx,ansmn;
int n,ctrd,fre,sum;
void dfs(int u,int p){
order.pb(u);
for(int v:graph[u]){
if(v==p) continue;
depth[v]=depth[u]+1;
dfs(v,u);
sz[u]+=sz[v];
}
}
int centroid(int u, int p){
for(int v:graph[u]){
if(v==p) continue;
if(sz[v]*2>n){
return centroid(v,u);
}
}
return u;
}
bool mindfs(int u,int p){// in subgraph of u is u used or no
bool posu=false;
int curr=-1;
for(int v:graph[u]){
if(v==p) continue;
bool flag=mindfs(v,u);
if(flag){
}else{
posu=true;
if(curr==-1){
ansmn[v]=u;
sum+=1;
curr=v;
}else{
ansmn[v]=curr;
sum+=2;
curr=v;
}
}
}
if(posu){
ansmn[u]=curr;
sum+=1;
return true;
}else{
return false;
}
}
int32_t main(){
speedIO;
int t,m,q;
//cin>>t;
t=1;
while(t--){
//string s;
cin>>n;
graph.clear();
graph.resize(n+1);
sz.clear();
sz.resize(n+1,1);
order.clear();
depth.clear();
depth.resize(n+1,0);
ansmx.clear();
ansmx.resize(n+1);
ansmn.clear();
ansmn.resize(n+1,0);
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
graph[u].pb(v);
graph[v].pb(u);
}
dfs(1,0);
ctrd=centroid(1,0);
depth.clear();
depth.resize(n+1,0);
order.clear();
dfs(ctrd,0);
int mx=0;
for(int i=1;i<=n;i++){
mx+=depth[i];
ansmx[order[i-1]]=order[(i-1+n+(n/2))%n];
}
mx*=2;
sum=0;
mindfs(ctrd,0);
if(ansmn[ctrd]==0){
int x=graph[ctrd][0];
ansmn[ctrd]=ansmn[x];
ansmn[x]=ctrd;
sum+=2;
}
cout<<sum<<' '<<mx<<'\n';
for(int i=1;i<=n;i++){
cout<<ansmn[i]<<' ';
}
cout<<'\n';
for(int i=1;i<=n;i++){
cout<<ansmx[i]<<' ';
}
cout<<'\n';
}
//}
return 0;
}
Compilation message
Village.cpp: In function 'int32_t main()':
Village.cpp:70:8: warning: unused variable 'm' [-Wunused-variable]
70 | int t,m,q;
| ^
Village.cpp:70:10: warning: unused variable 'q' [-Wunused-variable]
70 | int t,m,q;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
456 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
456 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
54 ms |
11716 KB |
Output is correct |
49 |
Correct |
50 ms |
12868 KB |
Output is correct |
50 |
Correct |
41 ms |
12740 KB |
Output is correct |
51 |
Correct |
35 ms |
10180 KB |
Output is correct |
52 |
Correct |
42 ms |
12604 KB |
Output is correct |
53 |
Correct |
37 ms |
11572 KB |
Output is correct |
54 |
Correct |
26 ms |
9164 KB |
Output is correct |
55 |
Correct |
57 ms |
18376 KB |
Output is correct |
56 |
Correct |
49 ms |
15460 KB |
Output is correct |
57 |
Correct |
47 ms |
14412 KB |
Output is correct |
58 |
Correct |
52 ms |
13696 KB |
Output is correct |
59 |
Correct |
44 ms |
13000 KB |
Output is correct |
60 |
Correct |
32 ms |
12876 KB |
Output is correct |
61 |
Correct |
37 ms |
12952 KB |
Output is correct |
62 |
Correct |
38 ms |
13256 KB |
Output is correct |
63 |
Correct |
35 ms |
12440 KB |
Output is correct |
64 |
Correct |
37 ms |
13252 KB |
Output is correct |
65 |
Correct |
35 ms |
13256 KB |
Output is correct |
66 |
Correct |
39 ms |
12496 KB |
Output is correct |
67 |
Correct |
27 ms |
9924 KB |
Output is correct |
68 |
Correct |
32 ms |
11260 KB |
Output is correct |
69 |
Correct |
37 ms |
13264 KB |
Output is correct |
70 |
Correct |
34 ms |
12488 KB |
Output is correct |
71 |
Correct |
25 ms |
9412 KB |
Output is correct |
72 |
Correct |
28 ms |
10704 KB |
Output is correct |
73 |
Correct |
35 ms |
13520 KB |
Output is correct |
74 |
Correct |
34 ms |
12240 KB |
Output is correct |
75 |
Correct |
49 ms |
13216 KB |
Output is correct |
76 |
Correct |
39 ms |
12740 KB |
Output is correct |
77 |
Correct |
41 ms |
12752 KB |
Output is correct |
78 |
Correct |
30 ms |
8904 KB |
Output is correct |
79 |
Correct |
33 ms |
10184 KB |
Output is correct |
80 |
Correct |
46 ms |
13260 KB |
Output is correct |
81 |
Correct |
40 ms |
13004 KB |
Output is correct |
82 |
Correct |
38 ms |
12912 KB |
Output is correct |