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>
#define pii pair<int,int>
#define ll long long
#define s second
using namespace std;
const int mxn=5e5+6;
vector<int> tree[mxn];
int k=1;
int _tm[mxn];
vector<pii> lf;
int root=1;
void dfs(int cur,int prev)
{
_tm[cur]=k++;
if(tree[cur].size()==1 && cur!=root)lf.push_back({_tm[cur],cur});
for(int i:tree[cur])
{
if(i!=prev)
{
dfs(i,cur);
}
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<n;i++)
{
int u,v;
cin>>u>>v;
tree[u].push_back(v);
tree[v].push_back(u);
}
for(int i=1;i<=n;i++)
{
if(tree[i].size()>1)
{
root=i;
break;
}
}
dfs(root,-1);
sort(lf.begin(),lf.end());
int r=lf.size();
cout<<(r+1)/2<<endl;
for(int i=0;i<(r+1)/2;i++)
{
if(i!=r-1-i)
cout<<lf[i].s<<" "<<lf[r-1-i].s<<endl;
else
cout<<root<<" "<<lf[i].s<<endl;
}
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... |