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 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 == 1 && 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) ;
}
dfs(1 , 1) ;
cout<< ans .size() <<"\n" ;
for(auto u : ans){
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... |