#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100000];
vector<int> son[100000];
int dp0[100000];
int dp1[100000];
int p[100000];
int group[100000];
int save[100000];
int nxt[100000];
int nxt2[100000];
int sz[100000];
typedef pair<int,int> P;
void dfs(int v,int prev) {
p[v]=prev;
sz[v]=1;
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt!=prev) {
son[v].push_back(nt);
dfs(nt,v);
sz[v]+=sz[nt];
}
}
for(int i=0;i<son[v].size();i++) {
int nt=son[v][i];
dp1[v]+=max(dp0[nt],dp1[nt]);
}
int mx=-1e6;
dp0[v]=1;
for(int i=0;i<son[v].size();i++) {
int nt=son[v][i];
mx=max(mx,dp1[nt]-dp0[nt]);
dp0[v]+=max(dp0[nt],dp1[nt]);
}
if (mx<0) {
dp0[v]+=mx;
}
}
int n;
vector<int> v;
void dfs2(int v,int type) {
if (type==0) {
group[v]=v;
save[v]=v;
int pos=-1;
int mx=-1e6;
for(int i=0;i<son[v].size();i++) {
int nt=son[v][i];
if (dp1[nt]-dp0[nt]>mx) {
mx=dp1[nt]-dp0[nt];
pos=i;
}
}
for(int i=0;i<son[v].size();i++) {
int nt=son[v][i];
if (dp1[nt]>dp0[nt]||pos==i) {
dfs2(nt,1);
}
else {
dfs2(nt,0);
}
}
}
else {
group[v]=group[p[v]];
nxt[v]=save[group[v]];
save[group[v]]=v;
for(int i=0;i<son[v].size();i++) {
int nt=son[v][i];
if (dp1[nt]>dp0[nt]) {
dfs2(nt,1);
}
else {
dfs2(nt,0);
}
}
}
}
int getcent(int v,int prev,int half) {
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt==prev) {
continue;
}
if (sz[nt]>half) {
return getcent(nt,v,half);
}
}
return v;
}
stack<int> s[100000];
void psh(int v,int type) {
s[type].push(v);
for(int i=0;i<son[v].size();i++) {
psh(son[v][i],type);
}
}
void dfs_sz(int v,int prev) {
sz[v]=1;
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt==prev) {
continue;
}
dfs_sz(nt,v);
sz[v]+=sz[nt];
}
}
int main(void) {
memset(save,-1,sizeof(save));
scanf("%d",&n);
if (n==2) {
printf("2 2\n2 1\n2 1");
return 0;
}
for(int i=1;i<n;i++) {
int u,v;
scanf("%d %d",&u,&v);
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs_sz(0,-1);
int cent=getcent(0,-1,n/2);
dfs(cent,-1);
printf("%d ",2*n-2*dp0[cent]);
long long ret=0;
for(int i=0;i<n;i++) {
if (i!=cent)
ret+=min(sz[i],n-sz[i])*2;
}
printf("%lld\n",ret);
priority_queue<P> pq;
for(int i=0;i<son[cent].size();i++) {
pq.push(P(sz[son[cent][i]],i));
psh(son[cent][i],i);
}
v.push_back(cent);
int prev=-1;
for(int i=1;i<n;i++) {
P now=pq.top();
P sav=P(-1,-1);
if (now.second==prev) {
sav=now;
pq.pop();
now=pq.top();
}
v.push_back(s[now.second].top());
s[now.second].pop();
pq.pop();
pq.push(P(now.first-1,now.second));
if (sav.second!=-1) {
pq.push(sav);
}
prev=now.second;
}
for(int i=0;i<n;i++) {
nxt2[v[i]]=v[(i+1)%n];
}
dfs2(cent,0);
for(int i=0;i<n;i++) {
if (save[i]!=-1) {
nxt[i]=save[i];
}
}
for(int i=0;i<n;i++) {
printf("%d ",nxt[i]+1);
}
printf("\n");
for(int i=0;i<n;i++) {
printf("%d ",nxt2[i]+1);
}
}
Compilation message
Village.cpp: In function 'void dfs(int, int)':
Village.cpp:19:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=0;i<son[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<son[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs2(int, int)':
Village.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<son[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<son[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0;i<son[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'int getcent(int, int, int)':
Village.cpp:86:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'void psh(int, int)':
Village.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<son[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'void dfs_sz(int, int)':
Village.cpp:109:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:145:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for(int i=0;i<son[cent].size();i++) {
| ~^~~~~~~~~~~~~~~~~
Village.cpp:121:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
121 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
Village.cpp:128:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
128 | scanf("%d %d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
72812 KB |
Output is correct |
2 |
Correct |
65 ms |
72812 KB |
Output is correct |
3 |
Correct |
50 ms |
72684 KB |
Output is correct |
4 |
Correct |
57 ms |
72812 KB |
Output is correct |
5 |
Correct |
57 ms |
72812 KB |
Output is correct |
6 |
Correct |
50 ms |
72812 KB |
Output is correct |
7 |
Correct |
52 ms |
72812 KB |
Output is correct |
8 |
Correct |
50 ms |
72812 KB |
Output is correct |
9 |
Correct |
53 ms |
72812 KB |
Output is correct |
10 |
Correct |
53 ms |
72812 KB |
Output is correct |
11 |
Correct |
51 ms |
72812 KB |
Output is correct |
12 |
Correct |
51 ms |
72832 KB |
Output is correct |
13 |
Correct |
61 ms |
72812 KB |
Output is correct |
14 |
Correct |
56 ms |
72812 KB |
Output is correct |
15 |
Correct |
52 ms |
72812 KB |
Output is correct |
16 |
Correct |
53 ms |
72812 KB |
Output is correct |
17 |
Correct |
58 ms |
72812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
72812 KB |
Output is correct |
2 |
Correct |
55 ms |
72812 KB |
Output is correct |
3 |
Correct |
54 ms |
72812 KB |
Output is correct |
4 |
Correct |
59 ms |
72812 KB |
Output is correct |
5 |
Correct |
60 ms |
72812 KB |
Output is correct |
6 |
Correct |
58 ms |
72812 KB |
Output is correct |
7 |
Correct |
52 ms |
72940 KB |
Output is correct |
8 |
Correct |
60 ms |
73004 KB |
Output is correct |
9 |
Correct |
55 ms |
72812 KB |
Output is correct |
10 |
Correct |
54 ms |
72812 KB |
Output is correct |
11 |
Correct |
55 ms |
72812 KB |
Output is correct |
12 |
Correct |
59 ms |
72812 KB |
Output is correct |
13 |
Correct |
56 ms |
72812 KB |
Output is correct |
14 |
Correct |
58 ms |
72812 KB |
Output is correct |
15 |
Correct |
57 ms |
72812 KB |
Output is correct |
16 |
Correct |
58 ms |
72812 KB |
Output is correct |
17 |
Correct |
56 ms |
72812 KB |
Output is correct |
18 |
Correct |
54 ms |
72812 KB |
Output is correct |
19 |
Correct |
65 ms |
72812 KB |
Output is correct |
20 |
Correct |
56 ms |
72928 KB |
Output is correct |
21 |
Correct |
57 ms |
72812 KB |
Output is correct |
22 |
Correct |
54 ms |
72812 KB |
Output is correct |
23 |
Correct |
55 ms |
72832 KB |
Output is correct |
24 |
Correct |
55 ms |
72812 KB |
Output is correct |
25 |
Correct |
49 ms |
72764 KB |
Output is correct |
26 |
Correct |
56 ms |
72944 KB |
Output is correct |
27 |
Correct |
60 ms |
72812 KB |
Output is correct |
28 |
Correct |
54 ms |
72812 KB |
Output is correct |
29 |
Correct |
58 ms |
72812 KB |
Output is correct |
30 |
Correct |
59 ms |
72816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
72812 KB |
Output is correct |
2 |
Correct |
65 ms |
72812 KB |
Output is correct |
3 |
Correct |
50 ms |
72684 KB |
Output is correct |
4 |
Correct |
57 ms |
72812 KB |
Output is correct |
5 |
Correct |
57 ms |
72812 KB |
Output is correct |
6 |
Correct |
50 ms |
72812 KB |
Output is correct |
7 |
Correct |
52 ms |
72812 KB |
Output is correct |
8 |
Correct |
50 ms |
72812 KB |
Output is correct |
9 |
Correct |
53 ms |
72812 KB |
Output is correct |
10 |
Correct |
53 ms |
72812 KB |
Output is correct |
11 |
Correct |
51 ms |
72812 KB |
Output is correct |
12 |
Correct |
51 ms |
72832 KB |
Output is correct |
13 |
Correct |
61 ms |
72812 KB |
Output is correct |
14 |
Correct |
56 ms |
72812 KB |
Output is correct |
15 |
Correct |
52 ms |
72812 KB |
Output is correct |
16 |
Correct |
53 ms |
72812 KB |
Output is correct |
17 |
Correct |
58 ms |
72812 KB |
Output is correct |
18 |
Correct |
57 ms |
72812 KB |
Output is correct |
19 |
Correct |
55 ms |
72812 KB |
Output is correct |
20 |
Correct |
54 ms |
72812 KB |
Output is correct |
21 |
Correct |
59 ms |
72812 KB |
Output is correct |
22 |
Correct |
60 ms |
72812 KB |
Output is correct |
23 |
Correct |
58 ms |
72812 KB |
Output is correct |
24 |
Correct |
52 ms |
72940 KB |
Output is correct |
25 |
Correct |
60 ms |
73004 KB |
Output is correct |
26 |
Correct |
55 ms |
72812 KB |
Output is correct |
27 |
Correct |
54 ms |
72812 KB |
Output is correct |
28 |
Correct |
55 ms |
72812 KB |
Output is correct |
29 |
Correct |
59 ms |
72812 KB |
Output is correct |
30 |
Correct |
56 ms |
72812 KB |
Output is correct |
31 |
Correct |
58 ms |
72812 KB |
Output is correct |
32 |
Correct |
57 ms |
72812 KB |
Output is correct |
33 |
Correct |
58 ms |
72812 KB |
Output is correct |
34 |
Correct |
56 ms |
72812 KB |
Output is correct |
35 |
Correct |
54 ms |
72812 KB |
Output is correct |
36 |
Correct |
65 ms |
72812 KB |
Output is correct |
37 |
Correct |
56 ms |
72928 KB |
Output is correct |
38 |
Correct |
57 ms |
72812 KB |
Output is correct |
39 |
Correct |
54 ms |
72812 KB |
Output is correct |
40 |
Correct |
55 ms |
72832 KB |
Output is correct |
41 |
Correct |
55 ms |
72812 KB |
Output is correct |
42 |
Correct |
49 ms |
72764 KB |
Output is correct |
43 |
Correct |
56 ms |
72944 KB |
Output is correct |
44 |
Correct |
60 ms |
72812 KB |
Output is correct |
45 |
Correct |
54 ms |
72812 KB |
Output is correct |
46 |
Correct |
58 ms |
72812 KB |
Output is correct |
47 |
Correct |
59 ms |
72816 KB |
Output is correct |
48 |
Correct |
175 ms |
81596 KB |
Output is correct |
49 |
Correct |
184 ms |
82444 KB |
Output is correct |
50 |
Correct |
174 ms |
82408 KB |
Output is correct |
51 |
Correct |
158 ms |
80356 KB |
Output is correct |
52 |
Correct |
185 ms |
82276 KB |
Output is correct |
53 |
Correct |
165 ms |
81384 KB |
Output is correct |
54 |
Correct |
112 ms |
81900 KB |
Output is correct |
55 |
Correct |
215 ms |
91492 KB |
Output is correct |
56 |
Correct |
214 ms |
86500 KB |
Output is correct |
57 |
Correct |
209 ms |
85176 KB |
Output is correct |
58 |
Correct |
206 ms |
84076 KB |
Output is correct |
59 |
Correct |
175 ms |
82284 KB |
Output is correct |
60 |
Correct |
157 ms |
81896 KB |
Output is correct |
61 |
Correct |
161 ms |
81284 KB |
Output is correct |
62 |
Correct |
214 ms |
81636 KB |
Output is correct |
63 |
Correct |
147 ms |
81252 KB |
Output is correct |
64 |
Correct |
166 ms |
81916 KB |
Output is correct |
65 |
Correct |
150 ms |
81892 KB |
Output is correct |
66 |
Correct |
143 ms |
81256 KB |
Output is correct |
67 |
Correct |
120 ms |
79076 KB |
Output is correct |
68 |
Correct |
133 ms |
80488 KB |
Output is correct |
69 |
Correct |
149 ms |
81892 KB |
Output is correct |
70 |
Correct |
139 ms |
81388 KB |
Output is correct |
71 |
Correct |
116 ms |
79084 KB |
Output is correct |
72 |
Correct |
130 ms |
80092 KB |
Output is correct |
73 |
Correct |
148 ms |
81892 KB |
Output is correct |
74 |
Correct |
143 ms |
81124 KB |
Output is correct |
75 |
Correct |
172 ms |
82244 KB |
Output is correct |
76 |
Correct |
171 ms |
81892 KB |
Output is correct |
77 |
Correct |
152 ms |
81768 KB |
Output is correct |
78 |
Correct |
117 ms |
78828 KB |
Output is correct |
79 |
Correct |
129 ms |
79720 KB |
Output is correct |
80 |
Correct |
194 ms |
82404 KB |
Output is correct |
81 |
Correct |
156 ms |
81764 KB |
Output is correct |
82 |
Correct |
157 ms |
81508 KB |
Output is correct |