#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;
lli nh[MAX+2],des[MAX+2],dir[MAX+2],usa[MAX+2];
vector<lli> hijos[MAX+2],orden,nuevo,grande;
lli dp[2][MAX+2];
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 MIN = INF;
lli nodo;
lli sum = 0;
for (auto h : hijos[pos]) {
if (h == padre) continue;
DFS(h,pos);
nh[pos] += nh[h];
if (dp[1][h] <= dp[0][h]) unos.push_back(h);
else if (dp[1][h]-dp[0][h] < MIN) {MIN = dp[1][h]-dp[0][h]; nodo = h;}
sum += min(dp[0][h], dp[1][h]);
}
if (nh[pos] == 1) {dp[1][pos]=2;dp[0][pos]=INF;}
else {
dp[1][pos] = sum+2;
if (unos.empty()){
sum += dp[1][nodo] - dp[0][nodo];
dp[0][pos] = sum;
}
else dp[0][pos] = sum;
}
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++];
}
void swapea(lli pos, lli padre, lli actual) {
lli nodo,MIN = INF;
vector<lli> unos;
if (pos != 1 && hijos[pos].size() == 1) return;
for (auto h : hijos[pos]) {
if (h == padre) continue;
if (dp[1][h] <= dp[0][h]) {unos.push_back(h); swapea(h,pos,1);}
else if (dp[1][h]-dp[0][h] < MIN) {MIN = dp[1][h]-dp[0][h]; nodo = h; swapea(h,pos,0);}
}
if (actual == 0 && unos.empty()) {unos.push_back(nodo);}
unos.push_back(pos);
if (unos.size() == 1) return;
rep(i,0,unos.size()-2) swap(dir[unos[i]],dir[unos[i+1]]);
}
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;
DFS(1,0);
cent = Find(1,0,n/2);
solve();
swapea(1,0,0);
cout << dp[0][1] << ' '<< M <<endl;
rep(i,1,n) cout << dir[i] << ' ';
cout << "\n";
rep(i,1,n) cout << des[i] << ' ';
}
Compilation message
Village.cpp: In function 'void solve()':
Village.cpp:79: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]
79 | if (menor == -1 || nuevo.size() < menor) menor = nuevo.size();
| ~~~~~~~~~~~~~^~~~~~~
Village.cpp:80: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]
80 | 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:92:5: note: in expansion of macro 'rep'
92 | 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:93:5: note: in expansion of macro 'rep'
93 | rep(i,0,orden.size()-mayor-1) des[orden[i]] = orden[cont++];
| ^~~
Village.cpp: In function 'void swapea(long long int, long long int, long long int)':
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:114:5: note: in expansion of macro 'rep'
114 | rep(i,0,unos.size()-2) swap(dir[unos[i]],dir[unos[i+1]]);
| ^~~
Village.cpp: In function 'void DFS(long long int, long long int)':
Village.cpp:54:44: warning: 'nodo' may be used uninitialized in this function [-Wmaybe-uninitialized]
54 | sum += dp[1][nodo] - dp[0][nodo];
| ~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
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 |
2624 KB |
Output is correct |
5 |
Correct |
2 ms |
2672 KB |
Output is correct |
6 |
Correct |
2 ms |
2676 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 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
11 |
Correct |
2 ms |
2672 KB |
Output is correct |
12 |
Partially correct |
2 ms |
2636 KB |
Partially 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 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
2 ms |
2676 KB |
Partially correct |
2 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
3 |
Partially correct |
2 ms |
2680 KB |
Partially correct |
4 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
5 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
6 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
7 |
Correct |
2 ms |
2892 KB |
Output is correct |
8 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
9 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
10 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
11 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
12 |
Correct |
2 ms |
2764 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Partially correct |
3 ms |
2764 KB |
Partially correct |
15 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
16 |
Partially correct |
3 ms |
2764 KB |
Partially correct |
17 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
18 |
Partially correct |
2 ms |
2808 KB |
Partially correct |
19 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
20 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
21 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
22 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
23 |
Partially correct |
3 ms |
2764 KB |
Partially correct |
24 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
25 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
26 |
Partially correct |
3 ms |
2808 KB |
Partially correct |
27 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
28 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
29 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
30 |
Partially correct |
2 ms |
2764 KB |
Partially 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 |
2624 KB |
Output is correct |
5 |
Correct |
2 ms |
2672 KB |
Output is correct |
6 |
Correct |
2 ms |
2676 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 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
11 |
Correct |
2 ms |
2672 KB |
Output is correct |
12 |
Partially correct |
2 ms |
2636 KB |
Partially 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 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
18 |
Partially correct |
2 ms |
2676 KB |
Partially correct |
19 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
20 |
Partially correct |
2 ms |
2680 KB |
Partially correct |
21 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
22 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
23 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
24 |
Correct |
2 ms |
2892 KB |
Output is correct |
25 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
26 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
27 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
28 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Partially correct |
3 ms |
2764 KB |
Partially correct |
32 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
33 |
Partially correct |
3 ms |
2764 KB |
Partially correct |
34 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
35 |
Partially correct |
2 ms |
2808 KB |
Partially correct |
36 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
37 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
38 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
39 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
40 |
Partially correct |
3 ms |
2764 KB |
Partially correct |
41 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
42 |
Partially correct |
2 ms |
2636 KB |
Partially correct |
43 |
Partially correct |
3 ms |
2808 KB |
Partially correct |
44 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
45 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
46 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
47 |
Partially correct |
2 ms |
2764 KB |
Partially correct |
48 |
Partially correct |
71 ms |
13120 KB |
Partially correct |
49 |
Partially correct |
85 ms |
14024 KB |
Partially correct |
50 |
Partially correct |
92 ms |
14032 KB |
Partially correct |
51 |
Partially correct |
58 ms |
11636 KB |
Partially correct |
52 |
Partially correct |
92 ms |
13812 KB |
Partially correct |
53 |
Partially correct |
77 ms |
12884 KB |
Partially correct |
54 |
Correct |
68 ms |
15180 KB |
Output is correct |
55 |
Correct |
141 ms |
28736 KB |
Output is correct |
56 |
Partially correct |
143 ms |
21040 KB |
Partially correct |
57 |
Partially correct |
91 ms |
18616 KB |
Partially correct |
58 |
Partially correct |
91 ms |
16600 KB |
Partially correct |
59 |
Partially correct |
85 ms |
14592 KB |
Partially correct |
60 |
Correct |
64 ms |
15272 KB |
Output is correct |
61 |
Partially correct |
68 ms |
14652 KB |
Partially correct |
62 |
Partially correct |
73 ms |
14560 KB |
Partially correct |
63 |
Partially correct |
77 ms |
13852 KB |
Partially correct |
64 |
Partially correct |
92 ms |
14084 KB |
Partially correct |
65 |
Partially correct |
76 ms |
14664 KB |
Partially correct |
66 |
Partially correct |
88 ms |
13640 KB |
Partially correct |
67 |
Partially correct |
47 ms |
11704 KB |
Partially correct |
68 |
Partially correct |
76 ms |
12648 KB |
Partially correct |
69 |
Partially correct |
71 ms |
15196 KB |
Partially correct |
70 |
Partially correct |
66 ms |
13612 KB |
Partially correct |
71 |
Partially correct |
46 ms |
10884 KB |
Partially correct |
72 |
Partially correct |
53 ms |
11960 KB |
Partially correct |
73 |
Partially correct |
71 ms |
14648 KB |
Partially correct |
74 |
Partially correct |
62 ms |
13528 KB |
Partially correct |
75 |
Partially correct |
83 ms |
14560 KB |
Partially correct |
76 |
Partially correct |
100 ms |
14392 KB |
Partially correct |
77 |
Partially correct |
80 ms |
14072 KB |
Partially correct |
78 |
Partially correct |
50 ms |
10376 KB |
Partially correct |
79 |
Partially correct |
76 ms |
11572 KB |
Partially correct |
80 |
Partially correct |
85 ms |
14556 KB |
Partially correct |
81 |
Partially correct |
89 ms |
14004 KB |
Partially correct |
82 |
Partially correct |
69 ms |
13936 KB |
Partially correct |