#include<bits/stdc++.h>
#define int long long
#define pii pair<long long, long long>
using namespace std;
int n, minn = 0, maxx = 0;
vector<vector<int> >G;
vector<int>minans;
vector<int>maxans;
bool dfs(int x, int p) {
//vector<int>c;
int y = x;
for (auto i : G[x]) {
if (i == p) continue;
bool can = dfs(i, x);
if (can) {
minn += 2;
minans[y] = i;
minans[i] = x;
y = i;
//c.push_back(i);
}
}
if (x == 0 && y == x) {
minn += 2;
minans[x] = minans[G[x][0]];
minans[G[x][0]] = x;
}
if (y == x) return 1;
else return 0;
//cout << x <<" " << c.size() << "\n";
/*for (int i = 0 ; i < c.size()/2 ; i++) {
minn += 4;
minans[c[i*2]] = c[i*2+1];
minans[c[i*2+1]] = c[i*2];
}
if (c.size() % 2 == 1) {
minn += 2;
minans[c.back()] = x;
minans[x] = c.back();
return 0;
} else {
if (x == 0) {
if (c.size() > 0) {
minans[c[0]] = x;
minans[x] = c[1];
} else {
minn += 2;
minans[x] = minans[G[x][0]];
minans[G[x][0]] = x;
}
return 0;
}
return 1;
}*/
}
int cnt;
int findc(int x, int p) {
int num = 1;
bool can = 1;
for (auto i : G[x]) {
if (i == p) continue;
int child = findc(i, x);
if (child > n/2) can = 0;
num += child;
}
if (n - num > n/2) can = 0;
if (can) cnt = x;
return num;
}
vector<int>arr;
void dis(int x, int p, int d) {
maxx += d*2;
arr.push_back(x);
for (auto i : G[x]) {
if (i == p) continue;
dis(i, x, d+1);
}
}
signed main() {
cin >> n;
G.resize(n); minans.resize(n); maxans.resize(n);
for (int i = 0 ; i < n ; i++) minans[i] = i;
int a, b;
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, -1);
findc(0, -1);
dis(cnt, -1, 0);
for (int i = 0 ; i < n ; i++) {
//cout << arr[i] <<" ";
maxans[arr[i]] = arr[(i+n/2)%n];
}
//cout <<"\n";
//assert(minn == ((n+1) / 2) * 2);
cout << minn << " " << maxx << "\n";
for (auto i : minans) cout << i+1 << " "; cout << "\n";
for (auto i : maxans) cout << i+1 << " "; cout << "\n";
}
Compilation message
Village.cpp: In function 'int main()':
Village.cpp:107:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
107 | for (auto i : minans) cout << i+1 << " "; cout << "\n";
| ^~~
Village.cpp:107:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
107 | for (auto i : minans) cout << i+1 << " "; cout << "\n";
| ^~~~
Village.cpp:108:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
108 | for (auto i : maxans) cout << i+1 << " "; cout << "\n";
| ^~~
Village.cpp:108:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
108 | for (auto i : maxans) cout << i+1 << " "; cout << "\n";
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
420 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
420 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
51 ms |
9092 KB |
Output is correct |
49 |
Correct |
58 ms |
10192 KB |
Output is correct |
50 |
Correct |
57 ms |
10184 KB |
Output is correct |
51 |
Correct |
42 ms |
7876 KB |
Output is correct |
52 |
Correct |
51 ms |
9928 KB |
Output is correct |
53 |
Correct |
47 ms |
9048 KB |
Output is correct |
54 |
Correct |
33 ms |
8540 KB |
Output is correct |
55 |
Correct |
61 ms |
17104 KB |
Output is correct |
56 |
Correct |
57 ms |
13512 KB |
Output is correct |
57 |
Correct |
56 ms |
12232 KB |
Output is correct |
58 |
Correct |
58 ms |
11232 KB |
Output is correct |
59 |
Correct |
53 ms |
10292 KB |
Output is correct |
60 |
Correct |
42 ms |
10316 KB |
Output is correct |
61 |
Correct |
45 ms |
10168 KB |
Output is correct |
62 |
Correct |
51 ms |
10396 KB |
Output is correct |
63 |
Correct |
43 ms |
9708 KB |
Output is correct |
64 |
Correct |
46 ms |
10436 KB |
Output is correct |
65 |
Correct |
46 ms |
10692 KB |
Output is correct |
66 |
Correct |
48 ms |
9932 KB |
Output is correct |
67 |
Correct |
37 ms |
7876 KB |
Output is correct |
68 |
Correct |
40 ms |
8904 KB |
Output is correct |
69 |
Correct |
46 ms |
10696 KB |
Output is correct |
70 |
Correct |
48 ms |
9916 KB |
Output is correct |
71 |
Correct |
38 ms |
7368 KB |
Output is correct |
72 |
Correct |
35 ms |
8392 KB |
Output is correct |
73 |
Correct |
47 ms |
10532 KB |
Output is correct |
74 |
Correct |
49 ms |
9864 KB |
Output is correct |
75 |
Correct |
51 ms |
10704 KB |
Output is correct |
76 |
Correct |
48 ms |
9848 KB |
Output is correct |
77 |
Correct |
51 ms |
10188 KB |
Output is correct |
78 |
Correct |
38 ms |
7008 KB |
Output is correct |
79 |
Correct |
37 ms |
8132 KB |
Output is correct |
80 |
Correct |
52 ms |
10504 KB |
Output is correct |
81 |
Correct |
53 ms |
10440 KB |
Output is correct |
82 |
Correct |
44 ms |
10184 KB |
Output is correct |