제출 #954436

#제출 시각아이디문제언어결과실행 시간메모리
954436amirhoseinfar1385Network (BOI15_net)C++17
100 / 100
382 ms47780 KiB
#include<bits/stdc++.h> using namespace std; const int maxn=500000+10; int n; vector<int>adj[maxn]; void vorod(){ cin>>n; for(int i=0;i<n-1;i++){ int u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } } vector<int>allv; vector<pair<int,int>>res; void dfs(int u,int par=-1){ if((int)adj[u].size()==1){ allv.push_back(u); return ; } for(auto x:adj[u]){ if(x!=par){ dfs(x,u); } } } void pre(){ for(int i=1;i<=n;i++){ if((int)adj[i].size()>1){ dfs(i); break; } } int sz=allv.size(); for(int i=0;i<sz/2;i++){ res.push_back(make_pair(allv[i],allv[i+sz/2])); } if(sz&1){ res.push_back(make_pair(allv[0],allv.back())); } } void khor(){ int sz=res.size(); cout<<sz<<"\n"; for(auto x:res){ cout<<x.first<<" "<<x.second<<"\n"; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("inp.txt","r",stdin); vorod(); pre(); khor(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...