#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
typedef pair <int, int> pii;
typedef pair <int, bool> pib;
typedef pair <int, pair <int, int> > pip;
typedef pair <pair <int, int>, int> ppi;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define MP make_pair
#define rep(i,a,b) for (long long i=(a);i<(b);i++)
#define rrep(i,a,b) for (long long i=(a);i>(b);i--)
#define SZ(x) ((long long) sizeof(x))
#define pb push_back
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define fileinp freopen("ain.txt","r",stdin);freopen("aout.txt","w",stdout);
const int dx[]={1,-1 ,0,0,1,-1,-1,1};
const int dy[]={0,0,1,-1,-1,-1,1,1};
const int mod=1e9+7;
const int mod2=998244353;
const int INF=(int)1e18;
const int N=100005;
int n,a,b,minans,maxans,cur,ct=1,curval;
int nb[N],minarr[N],maxarr[N],subtree[N];
bool check;
vector<int>adj[N],adj2[N];
vector<pii>temp;
void dfs(int x,int p){
subtree[x]=1;
for(auto it:adj[x]){
if(it!=p){
dfs(it,x);
subtree[x]+=subtree[it];
}
}
}
void dfs2(int x,int p,int chk){
//root tree at centroid
//adj2 is only for layer under centroid and itself
if(chk==-1){
adj2[x].pb(x);
}else{
adj2[chk].pb(x);
}
for(auto it:adj[x]){
if(it!=p){
dfs2(it,x,chk==-1?it:chk);
}
}
}
signed main(){
fastio;
//fileinp;
cin>>n;
rep(i,0,n-1){
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
nb[a]++;nb[b]++;
}
queue<int>q;
rep(i,1,n+1){
minarr[i]=i;
if(nb[i]==1){ //leaf
q.push(i);
}
}
while(!q.empty()){
cur=q.front();
q.pop();
if(minarr[cur]==cur){
curval=-1;
for(auto it:adj[cur]){
if(nb[it]!=0){
curval=it;
break;
}
}
if(curval==-1){
swap(minarr[cur],minarr[adj[cur][0]]);
}else if(curval!=-1){
swap(minarr[cur],minarr[curval]);
}
minans+=2;
}
for(auto it:adj[cur]){
if(nb[it]!=0){
nb[it]--;nb[cur]--;
if(nb[it]<=1){
q.push(it);
}
}
}
}
dfs(1,-1);
ct=1;
rep(i,1,n+1){
if(i>1){
maxans+=2*(min(subtree[i],n-subtree[i]));
}
check=0;
for(auto it:adj[i]){
if(subtree[it]<subtree[i]&&subtree[it]>n/2){
check=1;
}
}
if(n-subtree[i]>n/2){
check=1;
}
if(!check){
ct=i;//centroid
}
}
//cout<<ct<<'\n';
dfs2(ct,-1,-1);
rep(i,1,n+1){
if(adj2[i].size()>0){
temp.pb(MP(adj2[i].size(),i));
}
}
sort(all(temp),greater<pii>());
queue<int>q2;
rep(i,1,temp.size()){ //skip 0 (biggest subtree)
for(auto it:adj2[temp[i].se]){
q2.push(it);
}
}
rep(i,0,temp.size()){
for(auto it:adj2[temp[i].se]){
cur=q2.front();
q2.pop();
maxarr[it]=cur;
//cout<<it<<' '<<cur<<'\n';
q2.push(it);
}
}
cout<<minans<<' '<<maxans<<'\n';
rep(i,1,n+1){
cout<<minarr[i]<<' ';
}
cout<<'\n';
rep(i,1,n+1){
cout<<maxarr[i]<<' ';
}
return 0;
}
Compilation message
Village.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
10 | #pragma GCC optimization ("O3")
|
Village.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("unroll-loops")
|
Village.cpp: In function 'int main()':
Village.cpp:16:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | #define rep(i,a,b) for (long long i=(a);i<(b);i++)
| ^
Village.cpp:135:5: note: in expansion of macro 'rep'
135 | rep(i,1,temp.size()){ //skip 0 (biggest subtree)
| ^~~
Village.cpp:16:42: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | #define rep(i,a,b) for (long long i=(a);i<(b);i++)
| ^
Village.cpp:140:5: note: in expansion of macro 'rep'
140 | rep(i,0,temp.size()){
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
2 ms |
5028 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5152 KB |
Output is correct |
8 |
Correct |
3 ms |
5032 KB |
Output is correct |
9 |
Correct |
3 ms |
5096 KB |
Output is correct |
10 |
Correct |
3 ms |
5160 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 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 |
5036 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 |
5112 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
4 ms |
5028 KB |
Output is correct |
21 |
Correct |
3 ms |
5160 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5036 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 |
5160 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5028 KB |
Output is correct |
7 |
Correct |
2 ms |
5028 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
4 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
5024 KB |
Output is correct |
12 |
Correct |
3 ms |
5028 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
5028 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 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 |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5152 KB |
Output is correct |
25 |
Correct |
3 ms |
5032 KB |
Output is correct |
26 |
Correct |
3 ms |
5096 KB |
Output is correct |
27 |
Correct |
3 ms |
5160 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5204 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 |
5036 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 |
5112 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5028 KB |
Output is correct |
38 |
Correct |
3 ms |
5160 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5036 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
3 ms |
5160 KB |
Output is correct |
46 |
Correct |
3 ms |
5076 KB |
Output is correct |
47 |
Correct |
4 ms |
5076 KB |
Output is correct |
48 |
Correct |
62 ms |
14536 KB |
Output is correct |
49 |
Correct |
95 ms |
15728 KB |
Output is correct |
50 |
Correct |
81 ms |
15576 KB |
Output is correct |
51 |
Correct |
52 ms |
13284 KB |
Output is correct |
52 |
Correct |
70 ms |
15652 KB |
Output is correct |
53 |
Correct |
84 ms |
14408 KB |
Output is correct |
54 |
Correct |
42 ms |
12680 KB |
Output is correct |
55 |
Correct |
86 ms |
20852 KB |
Output is correct |
56 |
Correct |
94 ms |
18344 KB |
Output is correct |
57 |
Correct |
90 ms |
17360 KB |
Output is correct |
58 |
Correct |
97 ms |
16456 KB |
Output is correct |
59 |
Correct |
76 ms |
15780 KB |
Output is correct |
60 |
Correct |
75 ms |
19832 KB |
Output is correct |
61 |
Correct |
60 ms |
16736 KB |
Output is correct |
62 |
Correct |
59 ms |
15948 KB |
Output is correct |
63 |
Correct |
74 ms |
15560 KB |
Output is correct |
64 |
Correct |
62 ms |
16244 KB |
Output is correct |
65 |
Correct |
65 ms |
16096 KB |
Output is correct |
66 |
Correct |
64 ms |
15484 KB |
Output is correct |
67 |
Correct |
41 ms |
13580 KB |
Output is correct |
68 |
Correct |
51 ms |
14668 KB |
Output is correct |
69 |
Correct |
85 ms |
16228 KB |
Output is correct |
70 |
Correct |
51 ms |
15668 KB |
Output is correct |
71 |
Correct |
39 ms |
12876 KB |
Output is correct |
72 |
Correct |
51 ms |
13972 KB |
Output is correct |
73 |
Correct |
58 ms |
16292 KB |
Output is correct |
74 |
Correct |
52 ms |
15292 KB |
Output is correct |
75 |
Correct |
74 ms |
16032 KB |
Output is correct |
76 |
Correct |
80 ms |
15460 KB |
Output is correct |
77 |
Correct |
62 ms |
15668 KB |
Output is correct |
78 |
Correct |
52 ms |
12364 KB |
Output is correct |
79 |
Correct |
48 ms |
13464 KB |
Output is correct |
80 |
Correct |
80 ms |
16064 KB |
Output is correct |
81 |
Correct |
71 ms |
16288 KB |
Output is correct |
82 |
Correct |
57 ms |
15820 KB |
Output is correct |