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 ;
using ll = long long ;
const int N =1e5 + 7 ;
int n ;
vector<int> adj[N] ;
vector<pair<int , int> > ans ;
int root ;
int dfs(int x , int p){
int ls = 0;
vector<int> chs ;
for(auto u : adj[x]){
if(u == p)
continue ;
chs.push_back(dfs(u , x)) ;
}
while(chs.size() > 1){
ls = chs.back() ;
int u = chs.back() ; chs.pop_back() ;
int v = chs.back() ;chs.pop_back() ;
ans.push_back({u , v}) ;
}
if(x == root && chs.size()){
ans.push_back({x , chs.back() }) ;
}
if(chs.size())
return chs.back() ;
if(ls)return ls ;
return x ;
}
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
//freopen("in.in" , "r" , stdin) ;
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<pair<int , int> > res ;
int sz = 1e8 ;
for(int i =1 ; i <= n;i++){
root = i ;
ans.clear() ;
dfs(i , i) ;
if((int) ans.size() < sz){
sz = (int) ans.size() ;
res.clear() ;
res = ans ;
}
}
cout<< res .size() <<"\n" ;
for(auto u : res){
cout<< u.first<<" " << u.second <<"\n" ;
}
return 0 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |