This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* in the name of allah */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
const int N=5e5+7;
vector<int>vec[N],barg;
void dfs(int v,int p){
if(vec[v].size()==1) barg.pb(v);
for(auto u:vec[v]){
if(u!=p) dfs(u,v);
}
}
int main(){
ios:: sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
int u,v;
for(int i=0;i<n-1;i++){
cin>>u>>v;
vec[v].pb(u);
vec[u].pb(v);
}
if(n==2){
cout<<1<<endl<<1<<" "<<2;
return 0;
}
for(int i=1;i<=n;i++){
if(vec[i].size()!=1){
dfs(i,i);
break;
}
}
int x=barg.size()/2+(barg.size()%2);
cout<<x<<endl;
for(int i=0;i<x;i++){
cout<<barg[i]<<" "<<barg[(i+x)%barg.size()]<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |