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 <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int>sol;
void dfs(int a,int b,vector<vector<int> >& v){
for(int x:v[a]){
if(x!=b){
dfs(x,a,v);
}
}
if(v[a].size()==1)sol.push_back(a+1);
}
int main(){
int n,a,b;
cin>>n;
vector<vector<int> >v(n,vector<int>());
for(int i=0;i<n-1;i++){
cin>>a>>b;
a--;b--;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(0,-1,v);
int k=sol.size();
if(k%2){
cout<<k/2+1<<'\n';
for(int i=0;i<k-2;i+=2)cout<<sol[i]<<' '<<sol[i+1]<<'\n';
cout<<sol[k-1]<<' '<<sol[0]<<'\n';
}
else{
cout<<k/2<<'\n';
for(int i=0;i<k;i+=2)cout<<sol[i]<<' '<<sol[i+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... |