#include <bits/stdc++.h>
#define DIM 100010
#define INF 2000000000
using namespace std;
vector <int> L[DIM],w[DIM];
int v[DIM],fth[DIM],sp[DIM],f[DIM],viz[DIM];
pair <int,int> d[DIM];
int n,i,x,y,sol_min,k,idx,poz;
long long sol_max;
void dfs (int nod, int tata){
fth[nod] = tata;
sp[nod] = 1;
for (auto vecin : L[nod])
if (vecin != tata){
dfs (vecin,nod);
sp[nod] += sp[vecin];
}
if (nod != 1 && v[nod] == nod){
sol_min += 2;
swap (v[nod],v[fth[nod]]);
}
sol_max += 2 * min (sp[nod],n-sp[nod]);
}
int get_centroid (int nod, int tata){
int maxim = 0, heavy_son = 0;
for (auto vecin : L[nod]){
if (vecin == tata)
continue;
if (sp[vecin] > maxim)
maxim = sp[vecin], heavy_son = vecin;
}
if (maxim <= n/2 && n - sp[nod] <= n/2)
return nod;
else return get_centroid(heavy_son,nod);
}
void dfs2 (int nod, int idx){
w[idx].push_back(nod);
viz[nod] = 1;
f[nod] = idx;
for (auto vecin : L[nod])
if (!viz[vecin])
dfs2 (vecin,idx);
}
inline int cmp (int a, int b){
return (int)w[a].size() >= (int)w[b].size();
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n;
for (i=1;i<n;i++){
cin>>x>>y;
L[x].push_back(y);
L[y].push_back(x);
}
for (i=1;i<=n;i++)
v[i] = i;
dfs (1,0);
for (i=1;i<=n;i++)
if (v[i] == i){
sol_min += 2;
if (i != 1)
swap (v[i],v[fth[i]]);
else swap (v[i],v[L[1][0]]);
}
cout<<sol_min<<" "<<sol_max<<"\n";
for (i=1;i<=n;i++)
cout<<v[i]<<" ";
cout<<"\n";
memset (v,0,sizeof v);
int centroid = get_centroid(1,0);
viz[centroid] = 1;
w[centroid].push_back(centroid);
d[++k] = make_pair(-1,centroid);
int mini = n, poz;
for (auto vecin : L[centroid]){
dfs2 (vecin,vecin);
d[++k] = make_pair(-(int)w[vecin].size(),vecin);
//cout<<w[vecin].size()<<"\n";
}
sort (d+1,d+k+1);
for (int poz=2;poz<=k;poz++){
int i = w[d[poz].second].size()-1;
int j = w[d[poz-1].second].size()-1;
while (i >= 0){
v[w[d[poz].second][i]] = w[d[poz-1].second][j];
i--, j --;
}
}
idx = 2, poz = w[d[2].second].size() - 1;
for (i=1;i<=k;i++){
for (int j=0;j<w[d[i].second].size();j++){
v[w[d[i].second][j]] = w[d[idx].second][poz];
poz--;
if (poz < 0){
idx++;
if (idx > k)
idx = 1;
poz = w[d[idx].second].size()-1;
}
}
}
for (i=1;i<=n;i++)
cout<<v[i]<<" ";
/*for (i=1;i<=n;i++)
if (v[i] == 0)
cout<<"a";*/
return 0;
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:130:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for (int j=0;j<w[d[i].second].size();j++){
| ~^~~~~~~~~~~~~~~~~~~~~~
Village.cpp:101:9: warning: unused variable 'mini' [-Wunused-variable]
101 | int mini = n, poz;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5324 KB |
Output is correct |
3 |
Correct |
4 ms |
5324 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
4 ms |
5324 KB |
Output is correct |
6 |
Correct |
5 ms |
5380 KB |
Output is correct |
7 |
Correct |
6 ms |
5324 KB |
Output is correct |
8 |
Correct |
4 ms |
5324 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
4 ms |
5380 KB |
Output is correct |
11 |
Correct |
4 ms |
5380 KB |
Output is correct |
12 |
Correct |
5 ms |
5324 KB |
Output is correct |
13 |
Correct |
5 ms |
5372 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5324 KB |
Output is correct |
16 |
Correct |
4 ms |
5324 KB |
Output is correct |
17 |
Correct |
4 ms |
5380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5376 KB |
Output is correct |
2 |
Correct |
5 ms |
5324 KB |
Output is correct |
3 |
Correct |
5 ms |
5452 KB |
Output is correct |
4 |
Correct |
5 ms |
5452 KB |
Output is correct |
5 |
Correct |
5 ms |
5452 KB |
Output is correct |
6 |
Correct |
6 ms |
5452 KB |
Output is correct |
7 |
Correct |
5 ms |
5452 KB |
Output is correct |
8 |
Correct |
6 ms |
5452 KB |
Output is correct |
9 |
Correct |
5 ms |
5452 KB |
Output is correct |
10 |
Correct |
6 ms |
5452 KB |
Output is correct |
11 |
Correct |
5 ms |
5452 KB |
Output is correct |
12 |
Correct |
5 ms |
5452 KB |
Output is correct |
13 |
Correct |
5 ms |
5380 KB |
Output is correct |
14 |
Correct |
5 ms |
5452 KB |
Output is correct |
15 |
Correct |
5 ms |
5452 KB |
Output is correct |
16 |
Correct |
6 ms |
5384 KB |
Output is correct |
17 |
Correct |
5 ms |
5452 KB |
Output is correct |
18 |
Correct |
5 ms |
5452 KB |
Output is correct |
19 |
Correct |
5 ms |
5452 KB |
Output is correct |
20 |
Correct |
5 ms |
5388 KB |
Output is correct |
21 |
Correct |
5 ms |
5452 KB |
Output is correct |
22 |
Correct |
6 ms |
5452 KB |
Output is correct |
23 |
Correct |
5 ms |
5388 KB |
Output is correct |
24 |
Correct |
6 ms |
5452 KB |
Output is correct |
25 |
Correct |
5 ms |
5324 KB |
Output is correct |
26 |
Correct |
5 ms |
5452 KB |
Output is correct |
27 |
Correct |
5 ms |
5324 KB |
Output is correct |
28 |
Correct |
5 ms |
5452 KB |
Output is correct |
29 |
Correct |
5 ms |
5452 KB |
Output is correct |
30 |
Correct |
5 ms |
5452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
4 ms |
5324 KB |
Output is correct |
3 |
Correct |
4 ms |
5324 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
4 ms |
5324 KB |
Output is correct |
6 |
Correct |
5 ms |
5380 KB |
Output is correct |
7 |
Correct |
6 ms |
5324 KB |
Output is correct |
8 |
Correct |
4 ms |
5324 KB |
Output is correct |
9 |
Correct |
4 ms |
5324 KB |
Output is correct |
10 |
Correct |
4 ms |
5380 KB |
Output is correct |
11 |
Correct |
4 ms |
5380 KB |
Output is correct |
12 |
Correct |
5 ms |
5324 KB |
Output is correct |
13 |
Correct |
5 ms |
5372 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5324 KB |
Output is correct |
16 |
Correct |
4 ms |
5324 KB |
Output is correct |
17 |
Correct |
4 ms |
5380 KB |
Output is correct |
18 |
Correct |
4 ms |
5376 KB |
Output is correct |
19 |
Correct |
5 ms |
5324 KB |
Output is correct |
20 |
Correct |
5 ms |
5452 KB |
Output is correct |
21 |
Correct |
5 ms |
5452 KB |
Output is correct |
22 |
Correct |
5 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5452 KB |
Output is correct |
24 |
Correct |
5 ms |
5452 KB |
Output is correct |
25 |
Correct |
6 ms |
5452 KB |
Output is correct |
26 |
Correct |
5 ms |
5452 KB |
Output is correct |
27 |
Correct |
6 ms |
5452 KB |
Output is correct |
28 |
Correct |
5 ms |
5452 KB |
Output is correct |
29 |
Correct |
5 ms |
5452 KB |
Output is correct |
30 |
Correct |
5 ms |
5380 KB |
Output is correct |
31 |
Correct |
5 ms |
5452 KB |
Output is correct |
32 |
Correct |
5 ms |
5452 KB |
Output is correct |
33 |
Correct |
6 ms |
5384 KB |
Output is correct |
34 |
Correct |
5 ms |
5452 KB |
Output is correct |
35 |
Correct |
5 ms |
5452 KB |
Output is correct |
36 |
Correct |
5 ms |
5452 KB |
Output is correct |
37 |
Correct |
5 ms |
5388 KB |
Output is correct |
38 |
Correct |
5 ms |
5452 KB |
Output is correct |
39 |
Correct |
6 ms |
5452 KB |
Output is correct |
40 |
Correct |
5 ms |
5388 KB |
Output is correct |
41 |
Correct |
6 ms |
5452 KB |
Output is correct |
42 |
Correct |
5 ms |
5324 KB |
Output is correct |
43 |
Correct |
5 ms |
5452 KB |
Output is correct |
44 |
Correct |
5 ms |
5324 KB |
Output is correct |
45 |
Correct |
5 ms |
5452 KB |
Output is correct |
46 |
Correct |
5 ms |
5452 KB |
Output is correct |
47 |
Correct |
5 ms |
5452 KB |
Output is correct |
48 |
Correct |
116 ms |
12416 KB |
Output is correct |
49 |
Correct |
129 ms |
12992 KB |
Output is correct |
50 |
Correct |
126 ms |
13104 KB |
Output is correct |
51 |
Correct |
103 ms |
11424 KB |
Output is correct |
52 |
Correct |
135 ms |
12960 KB |
Output is correct |
53 |
Correct |
124 ms |
12372 KB |
Output is correct |
54 |
Correct |
80 ms |
12700 KB |
Output is correct |
55 |
Correct |
151 ms |
20420 KB |
Output is correct |
56 |
Correct |
145 ms |
16444 KB |
Output is correct |
57 |
Correct |
143 ms |
15300 KB |
Output is correct |
58 |
Correct |
137 ms |
14148 KB |
Output is correct |
59 |
Correct |
141 ms |
13076 KB |
Output is correct |
60 |
Correct |
144 ms |
16544 KB |
Output is correct |
61 |
Correct |
132 ms |
13992 KB |
Output is correct |
62 |
Correct |
120 ms |
13476 KB |
Output is correct |
63 |
Correct |
114 ms |
12920 KB |
Output is correct |
64 |
Correct |
131 ms |
13396 KB |
Output is correct |
65 |
Correct |
134 ms |
13632 KB |
Output is correct |
66 |
Correct |
114 ms |
12892 KB |
Output is correct |
67 |
Correct |
92 ms |
11716 KB |
Output is correct |
68 |
Correct |
99 ms |
12100 KB |
Output is correct |
69 |
Correct |
136 ms |
13516 KB |
Output is correct |
70 |
Correct |
119 ms |
12996 KB |
Output is correct |
71 |
Correct |
82 ms |
10912 KB |
Output is correct |
72 |
Correct |
90 ms |
11716 KB |
Output is correct |
73 |
Correct |
128 ms |
13572 KB |
Output is correct |
74 |
Correct |
105 ms |
12740 KB |
Output is correct |
75 |
Correct |
131 ms |
12840 KB |
Output is correct |
76 |
Correct |
134 ms |
13040 KB |
Output is correct |
77 |
Correct |
113 ms |
12936 KB |
Output is correct |
78 |
Correct |
87 ms |
10600 KB |
Output is correct |
79 |
Correct |
100 ms |
11456 KB |
Output is correct |
80 |
Correct |
137 ms |
12896 KB |
Output is correct |
81 |
Correct |
126 ms |
13128 KB |
Output is correct |
82 |
Correct |
122 ms |
13152 KB |
Output is correct |