This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> v;
vector<int> o;
vector<int> lea;
void dfs(int x, int p){
for(int y : v[x]){
if(y == p)continue;
dfs(y,x);
}
o.push_back(x);
}
int main(){
int n; cin >> n;
v.resize(n);
for(int i = 0; i < n-1; ++i){
int a, b; cin >> a >> b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
int root = 0;
for(int i = 0; i < n; ++i){
if(v[i].size() >= 2)root = i;
}
dfs(root,root);
for(int x : o){
if(v[x].size() == 1)lea.push_back(x);
}
int ln = lea.size();
cout << (ln+1)/2 << "\n";
for(int i = 0; i < ln/2; ++i){
cout << lea[i]+1 << " " << lea[(ln+1)/2+i]+1 << "\n";
}
if(ln&1){
cout << root+1 << " " << lea[ln/2]+1 << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |