#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;
for (auto i : G[x]) {
if (i == p) continue;
bool can = dfs(i, x);
if (can) {
c.push_back(i);
}
}
//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);
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";
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 'bool dfs(long long int, long long int)':
Village.cpp:21:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0 ; i < c.size()/2 ; i++) {
| ~~^~~~~~~~~~~~
Village.cpp: In function 'int main()':
Village.cpp:93:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
93 | for (auto i : minans) cout << i+1 << " "; cout << "\n";
| ^~~
Village.cpp:93:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
93 | for (auto i : minans) cout << i+1 << " "; cout << "\n";
| ^~~~
Village.cpp:94:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
94 | for (auto i : maxans) cout << i+1 << " "; cout << "\n";
| ^~~
Village.cpp:94:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
94 | for (auto i : maxans) cout << i+1 << " "; cout << "\n";
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Partially correct |
0 ms |
600 KB |
Partially correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Partially correct |
1 ms |
348 KB |
Partially correct |
16 |
Partially correct |
0 ms |
348 KB |
Partially correct |
17 |
Partially correct |
0 ms |
348 KB |
Partially correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
348 KB |
Partially correct |
2 |
Partially correct |
1 ms |
344 KB |
Partially correct |
3 |
Partially correct |
1 ms |
344 KB |
Partially correct |
4 |
Partially correct |
1 ms |
348 KB |
Partially correct |
5 |
Partially correct |
1 ms |
348 KB |
Partially correct |
6 |
Partially correct |
1 ms |
348 KB |
Partially correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Partially correct |
1 ms |
348 KB |
Partially correct |
9 |
Partially correct |
1 ms |
348 KB |
Partially correct |
10 |
Partially correct |
1 ms |
348 KB |
Partially correct |
11 |
Partially correct |
0 ms |
348 KB |
Partially correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Partially correct |
1 ms |
348 KB |
Partially correct |
15 |
Partially correct |
0 ms |
348 KB |
Partially correct |
16 |
Partially correct |
1 ms |
348 KB |
Partially correct |
17 |
Partially correct |
1 ms |
348 KB |
Partially correct |
18 |
Partially correct |
1 ms |
348 KB |
Partially correct |
19 |
Partially correct |
1 ms |
348 KB |
Partially correct |
20 |
Partially correct |
1 ms |
344 KB |
Partially correct |
21 |
Partially correct |
0 ms |
348 KB |
Partially correct |
22 |
Partially correct |
1 ms |
348 KB |
Partially correct |
23 |
Partially correct |
0 ms |
348 KB |
Partially correct |
24 |
Partially correct |
1 ms |
348 KB |
Partially correct |
25 |
Partially correct |
1 ms |
348 KB |
Partially correct |
26 |
Partially correct |
1 ms |
352 KB |
Partially correct |
27 |
Partially correct |
1 ms |
348 KB |
Partially correct |
28 |
Partially correct |
1 ms |
348 KB |
Partially correct |
29 |
Partially correct |
1 ms |
348 KB |
Partially correct |
30 |
Partially correct |
1 ms |
348 KB |
Partially 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 |
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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Partially correct |
0 ms |
600 KB |
Partially correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Partially correct |
1 ms |
348 KB |
Partially correct |
16 |
Partially correct |
0 ms |
348 KB |
Partially correct |
17 |
Partially correct |
0 ms |
348 KB |
Partially correct |
18 |
Partially correct |
0 ms |
348 KB |
Partially correct |
19 |
Partially correct |
1 ms |
344 KB |
Partially correct |
20 |
Partially correct |
1 ms |
344 KB |
Partially correct |
21 |
Partially correct |
1 ms |
348 KB |
Partially correct |
22 |
Partially correct |
1 ms |
348 KB |
Partially correct |
23 |
Partially correct |
1 ms |
348 KB |
Partially correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Partially correct |
1 ms |
348 KB |
Partially correct |
26 |
Partially correct |
1 ms |
348 KB |
Partially correct |
27 |
Partially correct |
1 ms |
348 KB |
Partially correct |
28 |
Partially correct |
0 ms |
348 KB |
Partially correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Partially correct |
1 ms |
348 KB |
Partially correct |
32 |
Partially correct |
0 ms |
348 KB |
Partially correct |
33 |
Partially correct |
1 ms |
348 KB |
Partially correct |
34 |
Partially correct |
1 ms |
348 KB |
Partially correct |
35 |
Partially correct |
1 ms |
348 KB |
Partially correct |
36 |
Partially correct |
1 ms |
348 KB |
Partially correct |
37 |
Partially correct |
1 ms |
344 KB |
Partially correct |
38 |
Partially correct |
0 ms |
348 KB |
Partially correct |
39 |
Partially correct |
1 ms |
348 KB |
Partially correct |
40 |
Partially correct |
0 ms |
348 KB |
Partially correct |
41 |
Partially correct |
1 ms |
348 KB |
Partially correct |
42 |
Partially correct |
1 ms |
348 KB |
Partially correct |
43 |
Partially correct |
1 ms |
352 KB |
Partially correct |
44 |
Partially correct |
1 ms |
348 KB |
Partially correct |
45 |
Partially correct |
1 ms |
348 KB |
Partially correct |
46 |
Partially correct |
1 ms |
348 KB |
Partially correct |
47 |
Partially correct |
1 ms |
348 KB |
Partially correct |
48 |
Partially correct |
66 ms |
9168 KB |
Partially correct |
49 |
Partially correct |
56 ms |
10188 KB |
Partially correct |
50 |
Partially correct |
63 ms |
10184 KB |
Partially correct |
51 |
Partially correct |
53 ms |
7880 KB |
Partially correct |
52 |
Partially correct |
61 ms |
10080 KB |
Partially correct |
53 |
Partially correct |
65 ms |
8908 KB |
Partially correct |
54 |
Correct |
30 ms |
9944 KB |
Output is correct |
55 |
Correct |
64 ms |
20172 KB |
Output is correct |
56 |
Partially correct |
99 ms |
15004 KB |
Partially correct |
57 |
Partially correct |
77 ms |
13412 KB |
Partially correct |
58 |
Partially correct |
73 ms |
11752 KB |
Partially correct |
59 |
Partially correct |
74 ms |
10588 KB |
Partially correct |
60 |
Correct |
72 ms |
11068 KB |
Output is correct |
61 |
Partially correct |
53 ms |
10264 KB |
Partially correct |
62 |
Partially correct |
49 ms |
10352 KB |
Partially correct |
63 |
Partially correct |
44 ms |
9944 KB |
Partially correct |
64 |
Partially correct |
50 ms |
10444 KB |
Partially correct |
65 |
Partially correct |
50 ms |
10696 KB |
Partially correct |
66 |
Partially correct |
58 ms |
9800 KB |
Partially correct |
67 |
Partially correct |
36 ms |
7828 KB |
Partially correct |
68 |
Partially correct |
45 ms |
9012 KB |
Partially correct |
69 |
Partially correct |
54 ms |
10704 KB |
Partially correct |
70 |
Partially correct |
42 ms |
9848 KB |
Partially correct |
71 |
Partially correct |
56 ms |
7492 KB |
Partially correct |
72 |
Partially correct |
66 ms |
8384 KB |
Partially correct |
73 |
Partially correct |
73 ms |
10580 KB |
Partially correct |
74 |
Partially correct |
79 ms |
9656 KB |
Partially correct |
75 |
Partially correct |
62 ms |
10576 KB |
Partially correct |
76 |
Correct |
62 ms |
9960 KB |
Output is correct |
77 |
Partially correct |
57 ms |
10188 KB |
Partially correct |
78 |
Partially correct |
39 ms |
7156 KB |
Partially correct |
79 |
Partially correct |
56 ms |
8132 KB |
Partially correct |
80 |
Partially correct |
78 ms |
10440 KB |
Partially correct |
81 |
Partially correct |
68 ms |
10300 KB |
Partially correct |
82 |
Partially correct |
80 ms |
10192 KB |
Partially correct |