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 ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << endl
#define all(x) x.begin(),x.end()
vector<int> adj[500010];
vector<int> v;
void dfs(int u,int p)
{
if(adj[u].size()==1) v.pb(u);
for(auto i : adj[u])
{
if(i!=p)
{
dfs(i,u);
}
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;cin>>n;
for(int i=1;i<n;i++)
{
int u,v;cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1;i<=n;i++)
{
if(adj[i].size()>1)
{
dfs(i,-1);
break;
}
}
vector<pii> ans;
int mid=v.size()/2;
for(int i=0;i<mid;i++)
{
ans.pb({v[i],v[i+mid]});
}
if(v.size()&1) ans.pb({v[0],v.back()});
cout<<ans.size()<<'\n';
for(auto i : ans) cout<<i.fi<<' '<<i.se<<'\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... |