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>
#pragma GCC optimize("Ofast")
using namespace std;
vector<vector<int>>adj;
vector<int>V;
void dfs(int v,int p){
if(adj[v].size()==1)V.push_back(v);
for(auto&u:adj[v]){
if(u==p)continue;
dfs(u,v);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
adj.resize(n);
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;u--;v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0,-1);
int sz=V.size();
cout<<(sz+1)/2<<"\n";
for(int i=0;i<sz/2;i++)cout<<V[i]+1<<" "<<V[i+(sz+1)/2]+1<<"\n";
if(sz&1)cout<<V[sz/2]+1<<" 1";
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... |