#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 5e5+5;
int n;
vector<int> adj[maxn];
int deg[maxn];
int subtree[maxn];
void dfs(int i, int p, int label) {
subtree[i] = label;
for (int j: adj[i]) {
if (j == p) continue;
dfs(j,i,label);
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
++deg[a];
++deg[b];
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> leafs;
int start = 1;
for (int i = 1; i <= n; i++) {
if (deg[i] == 1) leafs.push_back(i);
if (deg[i] > 1) {
start = i;
}
}
for (int j: adj[start]) {
dfs(j,j,j);
}
sort(leafs.begin(),leafs.end(),[](auto a, auto b) {
return subtree[a] < subtree[b];
});
vector<pair<int,int>> ans;
int l = 0, r = leafs.size()-1;
while (l <= r) {
if (l == r) {
ans.push_back({leafs[l],leafs[l+1]});
}
else {
ans.push_back({leafs[l],leafs[r]});
}
l++;
r--;
}
//output
cout << ans.size() << '\n';
for (auto p: ans) {
cout << p.first << ' ' << p.second << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12024 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12024 KB |
Breaking single line is causing network to disconnect. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12024 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12024 KB |
Breaking single line is causing network to disconnect. |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12024 KB |
Output is correct |
2 |
Incorrect |
13 ms |
12024 KB |
Breaking single line is causing network to disconnect. |
3 |
Halted |
0 ms |
0 KB |
- |