Submission #79533

#TimeUsernameProblemLanguageResultExecution timeMemory
79533Dat160601Network (BOI15_net)C++17
100 / 100
700 ms43484 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back int n,u,v,deg[500007],ans=0; vector <int> edge[500007],sv; void dfs(int u,int par){ bool leaf=true; for(int v:edge[u]){ if(v==par) continue; dfs(v,u); leaf=false; } if(leaf) ans++, sv.pb(u); } int main(){ ios_base::sync_with_stdio(0); cin>>n; for(int i=1;i<n;i++){ cin>>u>>v; deg[u]++, deg[v]++; edge[u].pb(v); edge[v].pb(u); } int root=-1; for(int i=1;i<=n;i++){ if(deg[i]>=2){ root=i; break; } } dfs(root,root); cout<<(ans+1)/2<<"\n"; if(ans&1){ int r=ans/2+1; for(int i=0;i<ans/2;i++){ cout<<sv[i]<<" "<<sv[r]<<"\n"; r++; } cout<<sv[ans/2]<<" "<<root<<"\n"; } else{ int r=ans/2; for(int i=0;i<ans/2;i++){ cout<<sv[i]<<" "<<sv[r]<<"\n"; r++; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...