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;
vector<int> ans;
vector<vector<int>> adj;
void dfs(int k,int pa){
if(adj[k].size()==1){
ans.push_back(k);
}
for(int j:adj[k]){
if(j==pa){
continue;
}
dfs(j,k);
}
}
int main(){
int n;cin>>n;
adj.resize(n);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;a--;b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0,0);
int m=ans.size();
for(int &j:ans){
j++;
}
cout<<(m+1)/2<<endl;
for(int i=0;i<m/2;i++){
cout<<ans[i]<<' '<<ans[i+m/2]<<endl;
}
if(m&1){
cout<<ans[0]<<' '<<ans[m-1]<<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... |