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 nn "\n"
#define x_x ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define intt int t; cin>>t; while(t--)
#define emp emplace_back
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
//#define a first
//#define b second
typedef long long ll;
int main()
{
x_x
int n; cin>>n; vector<int>adj[n+2]; vector<pair<int,int>>v,ch; int p[n+2];
for(int i=1,x,y; i<n; i++)cin>>x>>y, adj[x].emp(y), adj[y].emp(x),p[x]=y,p[y]=x;
for(int i=1; i<=n; i++)
if(sz(adj[i])==1)
{
ch.push_back({p[i],i});
}
sort(all(ch));
if(ch[0].first==ch.back().first)
{
cout<<sz(ch)/2+(sz(ch)%2)<<nn;
for(int i=0; i<sz(ch)-1; i+=2)cout<<ch[i].second<<' '<<ch[i+1].second<<nn;
if(sz(ch)%2)cout<<ch[0].second<<' '<<ch.back().second;
}
else
{
for(int i=0,j=0; i<sz(ch); i++)
{
if(!ch[i].first)continue;
while(ch[j].first==ch[i].first)j=(j+1)%sz(ch);
v.push_back({ch[i].second,ch[j].second}); ch[i].first=ch[j].first=0; j=(j+1)%sz(ch);
}
cout<<sz(v)<<nn;
for(auto i:v)cout<<i.first<< ' '<<i.second<<nn;
}
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... |