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;
#define int long long
const int maxn=5e5+10;
const int mod=998244353;
vector<int>g[maxn];
vector<int>v;
void dfs(int node,int par)
{
if(g[node].size()==1)
{
v.push_back(node);
return ;
}
for(auto ax:g[node])
{
if(ax==par)continue;
dfs(ax,node);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
///freopen("lexicografic.in","r",stdin);
///freopen("lexicografic.out","w",stdout);
int n;
cin>>n;
for(int i=0;i<n-1;i++)
{
int a,b;
cin>>a>>b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i=0;i<n;i++)
{
if(g[i].size()!=1)
{
dfs(i,-1);
break;
}
}
vector<pair<int,int>>ans;
int kol=v.size();
kol/=2;
for(int i=0;i<kol;i++)
{
ans.push_back({v[i],v[i+kol]});
}
if(v.size()%2==1)
{
ans.push_back({v[0],v[v.size()-1]});
}
cout<<ans.size()<<endl;
for(auto ax:ans)
{
cout<<ax.first+1<<" "<<ax.second+1<<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... |