이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |