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));
vector<int>df;
for(int i=0; i<sz(ch)-1; i++)
{
if(ch[i].first!=ch[i+1].first)
{
if(!df.empty()&&df.back()==ch[i].second)df.pop_back();
df.emp(ch[i].second), df.emp(ch[i+1].second);
ch[i].first=0;
if(i==(sz(ch)-2))ch[i+1].first=0;
}
else if(!df.empty()&&df.back()==ch[i].second)ch[i].first=0;
}
for(int i=0; i<sz(df)-1; i+=2)v.push_back({df[i],df[i+1]});
if(sz(df)%2)//v.push_back({df[0],df.back()});
{
for(auto&i:ch)
{
if(i.second==df.back()){i.first=1; break;}
}
}
int c=0;
for(int i=0,j=0; i<sz(ch); i++)
{
if(!ch[i].first)continue;
while(c!=-1&&(!ch[j].first||i==j))
{ if(c==sz(ch)){c=-1; break;} j=(j+1)%sz(ch); ++c;}
if(c==-1)
{if(i==j)j=(j+1)%sz(ch); }
else c=0;
v.push_back({ch[i].second,ch[j].second}), ch[j].first=0;
ch[i].first=0;
}
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... |