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;
#define pb push_back
int n,u,v,deg[500007],ans=0;
vector <int> edge[500007],sv;
void dfs(int u,int par){
bool leaf=true;
for(int v:edge[u]){
if(v==par) continue;
dfs(v,u);
leaf=false;
}
if(leaf) ans++, sv.pb(u);
}
int main(){
ios_base::sync_with_stdio(0);
cin>>n;
for(int i=1;i<n;i++){
cin>>u>>v;
deg[u]++, deg[v]++;
edge[u].pb(v);
edge[v].pb(u);
}
int root=-1;
for(int i=1;i<=n;i++){
if(deg[i]>=2){
root=i;
break;
}
}
dfs(root,root);
cout<<(ans+1)/2<<"\n";
if(ans&1){
int r=ans/2+1;
for(int i=0;i<ans/2;i++){
cout<<sv[i]<<" "<<sv[r]<<"\n";
r++;
}
cout<<sv[ans/2]<<" "<<root<<"\n";
}
else{
int r=ans/2;
for(int i=0;i<ans/2;i++){
cout<<sv[i]<<" "<<sv[r]<<"\n";
r++;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |