#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxN = 500005;
vector<int> adj[maxN], leafs;
void dfs(int v, int par) {
if (adj[v].size() == 1) {
leafs.push_back(v);
}
for(int u : adj[v]) {
if (u != par) {
dfs(u, v);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i = 0; i < n-1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
int ls = leafs.size();
if (ls % 2 == 0) {
cout << ls / 2 << "\n";
for(int i = 0; i < ls/2; i++) {
cout << leafs[i] << " " << leafs[ls/2+i] << "\n";
}
} else {
cout << ls / 2 + 1 << "\n";
for(int i = 0; i < ls/2; i++) {
cout << leafs[i] << " " << leafs[ls/2+i] << "\n";
}
cout << leafs.back() << " " << 1;
}
return 0;
}
//~ check for overflows
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
10 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12012 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
8 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12012 KB |
Output is correct |
10 |
Correct |
8 ms |
12012 KB |
Output is correct |
11 |
Correct |
9 ms |
12012 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12140 KB |
Output is correct |
17 |
Correct |
8 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12012 KB |
Output is correct |
19 |
Correct |
10 ms |
12140 KB |
Output is correct |
20 |
Correct |
8 ms |
12140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
10 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12012 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
8 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12012 KB |
Output is correct |
10 |
Correct |
8 ms |
12012 KB |
Output is correct |
11 |
Correct |
9 ms |
12012 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12140 KB |
Output is correct |
17 |
Correct |
8 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12012 KB |
Output is correct |
19 |
Correct |
10 ms |
12140 KB |
Output is correct |
20 |
Correct |
8 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12144 KB |
Output is correct |
22 |
Correct |
9 ms |
12268 KB |
Output is correct |
23 |
Correct |
9 ms |
12268 KB |
Output is correct |
24 |
Correct |
9 ms |
12268 KB |
Output is correct |
25 |
Correct |
9 ms |
12140 KB |
Output is correct |
26 |
Correct |
9 ms |
12140 KB |
Output is correct |
27 |
Correct |
9 ms |
12268 KB |
Output is correct |
28 |
Correct |
9 ms |
12140 KB |
Output is correct |
29 |
Correct |
9 ms |
12140 KB |
Output is correct |
30 |
Correct |
8 ms |
12140 KB |
Output is correct |
31 |
Correct |
9 ms |
12268 KB |
Output is correct |
32 |
Correct |
10 ms |
12140 KB |
Output is correct |
33 |
Incorrect |
8 ms |
12140 KB |
Breaking single line is causing network to disconnect. |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12140 KB |
Output is correct |
2 |
Correct |
10 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
8 ms |
12140 KB |
Output is correct |
5 |
Correct |
8 ms |
12012 KB |
Output is correct |
6 |
Correct |
9 ms |
12140 KB |
Output is correct |
7 |
Correct |
9 ms |
12140 KB |
Output is correct |
8 |
Correct |
8 ms |
12140 KB |
Output is correct |
9 |
Correct |
8 ms |
12012 KB |
Output is correct |
10 |
Correct |
8 ms |
12012 KB |
Output is correct |
11 |
Correct |
9 ms |
12012 KB |
Output is correct |
12 |
Correct |
9 ms |
12160 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12160 KB |
Output is correct |
16 |
Correct |
8 ms |
12140 KB |
Output is correct |
17 |
Correct |
8 ms |
12140 KB |
Output is correct |
18 |
Correct |
9 ms |
12012 KB |
Output is correct |
19 |
Correct |
10 ms |
12140 KB |
Output is correct |
20 |
Correct |
8 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12144 KB |
Output is correct |
22 |
Correct |
9 ms |
12268 KB |
Output is correct |
23 |
Correct |
9 ms |
12268 KB |
Output is correct |
24 |
Correct |
9 ms |
12268 KB |
Output is correct |
25 |
Correct |
9 ms |
12140 KB |
Output is correct |
26 |
Correct |
9 ms |
12140 KB |
Output is correct |
27 |
Correct |
9 ms |
12268 KB |
Output is correct |
28 |
Correct |
9 ms |
12140 KB |
Output is correct |
29 |
Correct |
9 ms |
12140 KB |
Output is correct |
30 |
Correct |
8 ms |
12140 KB |
Output is correct |
31 |
Correct |
9 ms |
12268 KB |
Output is correct |
32 |
Correct |
10 ms |
12140 KB |
Output is correct |
33 |
Incorrect |
8 ms |
12140 KB |
Breaking single line is causing network to disconnect. |
34 |
Halted |
0 ms |
0 KB |
- |