제출 #452114

#제출 시각아이디문제언어결과실행 시간메모리
452114JovanBNetwork (BOI15_net)C++17
100 / 100
624 ms54232 KiB
#include <bits/stdc++.h> using namespace std; int ukup=0; int leafs=0; int leafs1=0; vector <int> graf[500005]; bool visited[500005]; int listovi[500005]; int brlist[500005]; void dfs1(int v){ visited[v] = 1; bool nes = 0; for(auto c : graf[v]){ if(!visited[c]){dfs1(c); nes=1; brlist[v] += brlist[c];} } if(nes == 0) {leafs++; brlist[v] = 1;} } void dfs3(int v){ visited[v] = 1; bool nes = 0; for(auto c : graf[v]){ if(!visited[c]){dfs3(c); nes=1;} } if(nes == 0){leafs++; listovi[leafs] = v;} } int main(){ ios_base::sync_with_stdio(false),cin.tie(0),cerr.tie(0),cout.tie(0); cout.precision(10); cout<<fixed; int n; cin >> n; for(int i=1; i<n; i++){ int a, b; cin >> a >> b; graf[a].push_back(b); graf[b].push_back(a); } if(n == 2){cout << "1\n 1 2"; return 0;} dfs1(1); int x = 1; for(auto c : graf[1]){ if(brlist[c] > (brlist[1]/2)){ x = c; int par; par = 1; while(1){ if(graf[x].size() != 2) break; for(auto nesto : graf[x]){ if(nesto != par){par = x; x = nesto; break;} } } break; } } leafs = 0; for(int i=1; i<=n; i++) visited[i] = 0; dfs3(x); int r = 1; int br=0; while(r+(leafs/2) <= leafs){ br++; r++; } r = 1; cout << br << "\n"; while(r+(leafs/2) <= leafs){ cout << listovi[r] << " " << listovi[r+(leafs/2)] << "\n"; r++; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...