#include <bits/stdc++.h>
using namespace std;
vector<int>g[100000],sz(100000),vmin(100000),vmax(100000),temp,temp1;
int n;
long long int resmax=0,resmin=0;
void dfsmax(int a,int b,int c)
{
if(c==0)
{
temp.push_back(a);
}
else if(c==2)
{
temp1.push_back(a);
}
for(auto i:g[a])
{
if(i!=b&&c==1)
{
dfsmax(i,a,c);
resmax+=(2*min(sz[i],n-sz[i]));
}
else if(i!=b)
{
dfsmax(i,a,c);
}
}
}
void dfsmin(int a,int b)
{
for(auto i:g[a])
{
if(i!=b)
{
dfsmin(i,a);
if(vmin[i]==i)
{
swap(vmin[i],vmin[a]);
resmin+=2;
}
}
}
}
void dfs(int a,int b)
{
sz[a]=1;
for(auto i:g[a])
{
if(i!=b)
{
dfs(i,a);
sz[a]+=sz[i];
}
}
}
int findcen(int a)
{
int b=a;
while(true)
{
bool check=true;
for(auto i:g[a])
{
if(i!=b&&sz[i]*2>n)
{
b=a;
a=i;
check=false;
break;
}
}
if(check)
{
break;
}
}
return a;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int a,b;
cin>>n;
for(int i=0;i<n;i++)
{
vmax[i]=i;
vmin[i]=i;
}
for(int i=0;i<n-1;i++)
{
cin>>a>>b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0,0);
int cen=findcen(0);
dfs(cen,cen);
a=-1;b=-1;
dfsmin(cen,cen);
dfsmax(cen,cen,1);
for(auto i:g[cen])
{
if(sz[i]>b)
{
b=sz[i];
a=i;
}
}
dfsmax(a,cen,0);
for(auto i:g[cen])
{
if(i!=a)
{
dfsmax(i,cen,2);
}
}
if(temp.size()>temp1.size())temp1.push_back(cen);
else{temp.push_back(cen);}
for(int i=0;i<min(temp.size(),temp1.size());i++)
{
vmax[temp1[i]]=temp[i];
vmax[temp[i]]=temp1[i];
}
if(vmax[cen]==cen)
{
swap(vmax[cen],vmax[g[cen][0]]);
}
if(vmin[cen]==cen)
{
swap(vmin[cen],vmin[g[cen][0]]);
resmin+=2;
}
cout<<resmin<<" "<<resmax<<"\n";
for(int i=0;i<n;i++)
{
cout<<vmin[i]+1<<" ";
}
cout<<"\n";
for(int i=0;i<n;i++)
{
cout<<vmax[i]+1<<" ";
}
return 0;
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:122:18: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
122 | for(int i=0;i<min(temp.size(),temp1.size());i++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3928 KB |
Output is correct |
3 |
Correct |
2 ms |
4000 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
8 |
Correct |
2 ms |
3928 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
2 ms |
3932 KB |
Output is correct |
14 |
Partially correct |
2 ms |
3748 KB |
Partially correct |
15 |
Correct |
1 ms |
3996 KB |
Output is correct |
16 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
2 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
3 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
4 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
5 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
6 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
7 |
Correct |
2 ms |
3928 KB |
Output is correct |
8 |
Partially correct |
2 ms |
3848 KB |
Partially correct |
9 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
13 |
Correct |
2 ms |
3996 KB |
Output is correct |
14 |
Partially correct |
3 ms |
3928 KB |
Partially correct |
15 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
16 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
17 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
18 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
19 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
20 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
21 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
22 |
Partially correct |
2 ms |
4184 KB |
Partially correct |
23 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
24 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
25 |
Correct |
2 ms |
4000 KB |
Output is correct |
26 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
27 |
Correct |
2 ms |
3928 KB |
Output is correct |
28 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
29 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
30 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3928 KB |
Output is correct |
2 |
Correct |
2 ms |
3928 KB |
Output is correct |
3 |
Correct |
2 ms |
4000 KB |
Output is correct |
4 |
Correct |
2 ms |
3932 KB |
Output is correct |
5 |
Correct |
1 ms |
3932 KB |
Output is correct |
6 |
Correct |
2 ms |
3932 KB |
Output is correct |
7 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
8 |
Correct |
2 ms |
3928 KB |
Output is correct |
9 |
Correct |
2 ms |
3932 KB |
Output is correct |
10 |
Correct |
2 ms |
3932 KB |
Output is correct |
11 |
Correct |
2 ms |
3932 KB |
Output is correct |
12 |
Correct |
2 ms |
3932 KB |
Output is correct |
13 |
Correct |
2 ms |
3932 KB |
Output is correct |
14 |
Partially correct |
2 ms |
3748 KB |
Partially correct |
15 |
Correct |
1 ms |
3996 KB |
Output is correct |
16 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
19 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
20 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
21 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
22 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
23 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
24 |
Correct |
2 ms |
3928 KB |
Output is correct |
25 |
Partially correct |
2 ms |
3848 KB |
Partially correct |
26 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
27 |
Correct |
2 ms |
3932 KB |
Output is correct |
28 |
Correct |
2 ms |
3932 KB |
Output is correct |
29 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
30 |
Correct |
2 ms |
3996 KB |
Output is correct |
31 |
Partially correct |
3 ms |
3928 KB |
Partially correct |
32 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
33 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
34 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
35 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
36 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
37 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
38 |
Partially correct |
3 ms |
3932 KB |
Partially correct |
39 |
Partially correct |
2 ms |
4184 KB |
Partially correct |
40 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
41 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
42 |
Correct |
2 ms |
4000 KB |
Output is correct |
43 |
Partially correct |
2 ms |
3928 KB |
Partially correct |
44 |
Correct |
2 ms |
3928 KB |
Output is correct |
45 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
46 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
47 |
Partially correct |
2 ms |
3932 KB |
Partially correct |
48 |
Partially correct |
48 ms |
9256 KB |
Partially correct |
49 |
Partially correct |
46 ms |
9608 KB |
Partially correct |
50 |
Partially correct |
44 ms |
9604 KB |
Partially correct |
51 |
Partially correct |
38 ms |
8452 KB |
Partially correct |
52 |
Partially correct |
45 ms |
9416 KB |
Partially correct |
53 |
Partially correct |
39 ms |
9044 KB |
Partially correct |
54 |
Correct |
30 ms |
9552 KB |
Output is correct |
55 |
Correct |
58 ms |
15544 KB |
Output is correct |
56 |
Correct |
61 ms |
12460 KB |
Output is correct |
57 |
Partially correct |
54 ms |
11348 KB |
Partially correct |
58 |
Correct |
64 ms |
10392 KB |
Output is correct |
59 |
Partially correct |
44 ms |
9556 KB |
Partially correct |
60 |
Partially correct |
34 ms |
9860 KB |
Partially correct |
61 |
Partially correct |
33 ms |
9936 KB |
Partially correct |
62 |
Partially correct |
41 ms |
10064 KB |
Partially correct |
63 |
Partially correct |
34 ms |
9740 KB |
Partially correct |
64 |
Partially correct |
42 ms |
9928 KB |
Partially correct |
65 |
Partially correct |
35 ms |
9960 KB |
Partially correct |
66 |
Partially correct |
31 ms |
9688 KB |
Partially correct |
67 |
Partially correct |
26 ms |
8664 KB |
Partially correct |
68 |
Partially correct |
29 ms |
9328 KB |
Partially correct |
69 |
Partially correct |
35 ms |
10060 KB |
Partially correct |
70 |
Partially correct |
34 ms |
9672 KB |
Partially correct |
71 |
Partially correct |
25 ms |
8396 KB |
Partially correct |
72 |
Partially correct |
30 ms |
8920 KB |
Partially correct |
73 |
Partially correct |
36 ms |
10072 KB |
Partially correct |
74 |
Partially correct |
34 ms |
9676 KB |
Partially correct |
75 |
Partially correct |
47 ms |
9552 KB |
Partially correct |
76 |
Partially correct |
40 ms |
9552 KB |
Partially correct |
77 |
Partially correct |
41 ms |
9804 KB |
Partially correct |
78 |
Partially correct |
26 ms |
7888 KB |
Partially correct |
79 |
Partially correct |
31 ms |
8664 KB |
Partially correct |
80 |
Partially correct |
47 ms |
9556 KB |
Partially correct |
81 |
Partially correct |
37 ms |
9932 KB |
Partially correct |
82 |
Partially correct |
36 ms |
10000 KB |
Partially correct |