#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define INF 1000000000000
#define MAX 100000
lli n,a,b,M,cent, summin;
lli nh[MAX+2],des[MAX+2],dir[MAX+2],desmin[MAX+2];
vector<lli> hijos[MAX+2],orden,nuevo,grande;
lli Find(lli pos, lli padre,lli mitad) {
lli grande = -1;
for(auto h : hijos[pos]) {
if (padre == h) continue;
if (nh[h] > mitad) grande = h;
}
if (grande == -1) return pos;
else return Find(grande,pos,mitad);
}
void DFS(lli pos, lli padre) {
nh[pos] = 1;
vector<lli> unos;
lli nodo;
for (auto h : hijos[pos]) {
if (h == padre) continue;
DFS(h,pos);
nh[pos] += nh[h];
if (nh[h] == 1 || desmin[h] == h){
swap(desmin[h], desmin[pos]);
summin += 2;
}
nodo = h;
}
if (desmin[pos] == pos && pos == 1){
swap(desmin[nodo], desmin[pos]);
summin += 2;
}
lli x = min(nh[pos],n-nh[pos]);
M += x*2;
}
void agrega(lli pos, lli padre) {
nuevo.push_back(pos);
for (auto h : hijos[pos]) if (h != padre) agrega(h,pos);
}
void solve() {
lli menor,mayor;
menor = mayor = -1;
orden.push_back(cent);
for (auto h : hijos[cent]) {
nuevo.clear();
agrega(h,cent);
if (menor == -1 || nuevo.size() < menor) menor = nuevo.size();
if (mayor == -1 || nuevo.size() > mayor) {
swap(nuevo,grande);
mayor = grande.size();
}
if (nuevo.empty()) continue;
for (auto act : nuevo) orden.push_back(act);
}
for (auto act : grande) orden.push_back(act);
lli cont = 0;
rep(i,orden.size()-mayor,orden.size()-1) des[orden[i]] = orden[cont++];
rep(i,0,orden.size()-mayor-1) des[orden[i]] = orden[cont++];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
rep(i,1,n-1) {
cin >> a >> b;
hijos[a].push_back(b);
hijos[b].push_back(a);
dir[i] = i;
}
dir[n] = n;
rep(i, 1, n) desmin[i] = i;
DFS(1,0);
cent = Find(1,0,n/2);
solve();
cout << summin << ' '<< M <<endl;
rep(i,1,n) cout << desmin[i] << ' ';
cout << "\n";
rep(i,1,n) cout << des[i] << ' ';
}
Compilation message
Village.cpp: In function 'void solve()':
Village.cpp:73:41: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
73 | if (menor == -1 || nuevo.size() < menor) menor = nuevo.size();
| ~~~~~~~~~~~~~^~~~~~~
Village.cpp:74:41: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
74 | if (mayor == -1 || nuevo.size() > mayor) {
| ~~~~~~~~~~~~~^~~~~~~
Village.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(i,a,b) for (int i = (a); i <= (b); i++)
| ^
Village.cpp:86:5: note: in expansion of macro 'rep'
86 | rep(i,orden.size()-mayor,orden.size()-1) des[orden[i]] = orden[cont++];
| ^~~
Village.cpp:4:40: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
4 | #define rep(i,a,b) for (int i = (a); i <= (b); i++)
| ^
Village.cpp:87:5: note: in expansion of macro 'rep'
87 | rep(i,0,orden.size()-mayor-1) des[orden[i]] = orden[cont++];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
2 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2892 KB |
Output is correct |
7 |
Correct |
2 ms |
2812 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2764 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
2764 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
2 ms |
2764 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Correct |
2 ms |
2764 KB |
Output is correct |
18 |
Correct |
2 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
3 ms |
2764 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2764 KB |
Output is correct |
23 |
Correct |
2 ms |
2764 KB |
Output is correct |
24 |
Correct |
2 ms |
2764 KB |
Output is correct |
25 |
Correct |
2 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2684 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
2636 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
2 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2764 KB |
Output is correct |
23 |
Correct |
2 ms |
2892 KB |
Output is correct |
24 |
Correct |
2 ms |
2812 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2764 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
2 ms |
2764 KB |
Output is correct |
34 |
Correct |
2 ms |
2764 KB |
Output is correct |
35 |
Correct |
2 ms |
2764 KB |
Output is correct |
36 |
Correct |
2 ms |
2764 KB |
Output is correct |
37 |
Correct |
3 ms |
2764 KB |
Output is correct |
38 |
Correct |
2 ms |
2764 KB |
Output is correct |
39 |
Correct |
2 ms |
2764 KB |
Output is correct |
40 |
Correct |
2 ms |
2764 KB |
Output is correct |
41 |
Correct |
2 ms |
2764 KB |
Output is correct |
42 |
Correct |
2 ms |
2636 KB |
Output is correct |
43 |
Correct |
2 ms |
2684 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2764 KB |
Output is correct |
46 |
Correct |
2 ms |
2764 KB |
Output is correct |
47 |
Correct |
2 ms |
2764 KB |
Output is correct |
48 |
Correct |
65 ms |
11284 KB |
Output is correct |
49 |
Correct |
77 ms |
11932 KB |
Output is correct |
50 |
Correct |
75 ms |
12128 KB |
Output is correct |
51 |
Correct |
60 ms |
10192 KB |
Output is correct |
52 |
Correct |
72 ms |
11816 KB |
Output is correct |
53 |
Correct |
64 ms |
11188 KB |
Output is correct |
54 |
Correct |
39 ms |
10784 KB |
Output is correct |
55 |
Correct |
92 ms |
19356 KB |
Output is correct |
56 |
Correct |
88 ms |
15808 KB |
Output is correct |
57 |
Correct |
85 ms |
14524 KB |
Output is correct |
58 |
Correct |
90 ms |
13584 KB |
Output is correct |
59 |
Correct |
80 ms |
12572 KB |
Output is correct |
60 |
Correct |
59 ms |
11872 KB |
Output is correct |
61 |
Correct |
65 ms |
12096 KB |
Output is correct |
62 |
Correct |
66 ms |
12736 KB |
Output is correct |
63 |
Correct |
62 ms |
11924 KB |
Output is correct |
64 |
Correct |
68 ms |
12172 KB |
Output is correct |
65 |
Correct |
64 ms |
12660 KB |
Output is correct |
66 |
Correct |
63 ms |
11888 KB |
Output is correct |
67 |
Correct |
47 ms |
9812 KB |
Output is correct |
68 |
Correct |
54 ms |
11048 KB |
Output is correct |
69 |
Correct |
66 ms |
13248 KB |
Output is correct |
70 |
Correct |
60 ms |
11760 KB |
Output is correct |
71 |
Correct |
48 ms |
9580 KB |
Output is correct |
72 |
Correct |
52 ms |
10432 KB |
Output is correct |
73 |
Correct |
68 ms |
12728 KB |
Output is correct |
74 |
Correct |
58 ms |
11764 KB |
Output is correct |
75 |
Correct |
95 ms |
12556 KB |
Output is correct |
76 |
Correct |
74 ms |
12448 KB |
Output is correct |
77 |
Correct |
74 ms |
12336 KB |
Output is correct |
78 |
Correct |
44 ms |
9112 KB |
Output is correct |
79 |
Correct |
55 ms |
10040 KB |
Output is correct |
80 |
Correct |
76 ms |
12624 KB |
Output is correct |
81 |
Correct |
67 ms |
12088 KB |
Output is correct |
82 |
Correct |
64 ms |
12032 KB |
Output is correct |