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;
const int maxn=500000+10;
int n;
vector<int>adj[maxn];
void vorod(){
cin>>n;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
}
vector<int>allv;
vector<pair<int,int>>res;
void dfs(int u,int par=-1){
if((int)adj[u].size()==1){
allv.push_back(u);
return ;
}
for(auto x:adj[u]){
if(x!=par){
dfs(x,u);
}
}
}
void pre(){
for(int i=1;i<=n;i++){
if((int)adj[i].size()>1){
dfs(i);
break;
}
}
int sz=allv.size();
for(int i=0;i<sz/2;i++){
res.push_back(make_pair(allv[i],allv[i+sz/2]));
}
if(sz&1){
res.push_back(make_pair(allv[0],allv.back()));
}
}
void khor(){
int sz=res.size();
cout<<sz<<"\n";
for(auto x:res){
cout<<x.first<<" "<<x.second<<"\n";
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("inp.txt","r",stdin);
vorod();
pre();
khor();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |