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 N 500005
using namespace std;
int n,m,i,j,a,b,t,x,ok,l[N],f[N];
bool r[N];
vector<int> v[N];
vector<pair<int,int>> sol;
int main()
{
cin>>n;
for(i=1;i<n;++i)
{
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(i=2;i<=n;++i)
{
if(v[i].size()==1)
{
++t;
f[t]=i;
l[i]=t;
}
}
m=v[1].size();
for(i=0;i<m;++i)
{
x=v[1][i];
r[x]=1;
}
for(x=1;x<=n;++x)
{
if(r[x]) continue;
if(l[x])
{
swap(f[t],f[l[x]]);
ok=1;
break;
}
}
for(i=2;i<=t;i+=2)
{
sol.push_back({f[i-1],f[i]});
}
if(ok)
{
sol.push_back({1,f[t]});
}
m=sol.size();
cout<<m<<'\n';
for(i=0;i<m;++i)
{
cout<<sol[i].first<<' '<<sol[i].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... |