제출 #987643

#제출 시각아이디문제언어결과실행 시간메모리
987643nihonNetwork (BOI15_net)C++14
0 / 100
17 ms30296 KiB
#include <bits/stdc++.h> #define N 500005 using namespace std; int n,m,i,j,a,b,t,x,ok,l[N],f[N]; bool r[N]; vector<int> v[N]; vector<pair<int,int>> sol; bool cmp(int a,int b) { return (v[a][0]<v[b][0])||(v[a][0]==v[b][0] && r[a]<r[b]); } int main() { cin>>n; for(i=1;i<n;++i) { cin>>a>>b; v[a].push_back(b); v[b].push_back(a); } for(i=2;i<=n;++i) { if(v[i].size()==1) { ++t; f[t]=i; l[i]=t; } } m=v[1].size(); if(1) { for(i=0;i<m;++i) { x=v[1][i]; r[x]=1; } for(x=1;x<=n;++x) { if(r[x]) continue; if(l[x]) { swap(f[t],f[l[x]]); ok=1; break; } } } if(t%2==1 || m==1) {sol.push_back({f[t],1}); if(!(m==1 && t%2==0)) f[t]=0;} sort(f+1,f+t+1,cmp); for(i=2;i<=t;) { if(v[f[i]][0]!=v[f[i-1]][0]) { sol.push_back({f[i],f[i-1]}); f[i]=f[i-1]=0; i+=2; } else i++; } int st=0,dr=1; while(st<=n-2) { st++; while(!f[st] && st<=n-2) ++st; if(dr<=st) dr=st+1; while(!f[dr] && dr<=n-1) ++dr; if(f[st] && f[dr]) { sol.push_back({f[st],f[dr]}); f[st]=f[dr]=0; } } m=sol.size(); cout<<m<<'\n'; for(i=0;i<m;++i) { cout<<sol[i].first<<' '<<sol[i].second<<'\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...