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);
}
dfs(0,0);
for(int x : o){
if(v[x].size() == 1)lea.push_back(x);
}
int ln = lea.size();
bool ok = 0;
cout << (ln+1)/2 << "\n";
if(lea[ln-1] == 0){
lea.pop_back();
ln--;
cout << 1 << " " << lea[ln/2]+1 << "\n";
ok = 1;
}
for(int i = 0; i < ln/2; ++i){
cout << lea[i]+1 << " " << lea[ln-1-i]+1 << "\n";
}
if((ln&1) && !ok){
cout << lea[0]+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... |