제출 #96090

#제출 시각아이디문제언어결과실행 시간메모리
96090andrei110901Network (BOI15_net)C++14
63 / 100
2052 ms43556 KiB
#include <bits/stdc++.h> using namespace std; vector <vector <int>> ad; vector <int> sub; int nrf, cf; void dfs(int nod, int par){ if(ad[nod].size() == 1) sub[nod] = 1; for(auto fiu : ad[nod]) if(fiu != par){ dfs(fiu, nod); sub[nod] += sub[fiu]; } } int get_centroid(int nod, int par){ for(auto fiu : ad[nod]) if(fiu != par && sub[fiu] > nrf / 2) return get_centroid(fiu, nod); return nod; } bool cmp(const vector <int> &a, const vector <int> &b){ return a.size() > b.size(); } void Frunza(int nod, int par, vector <int> &v){ if(ad[nod].size() == 1){ v.push_back(nod); return; } for(auto fiu : ad[nod]) if(fiu != par) Frunza(fiu, nod, v); } int main() { int n, x, y; cin >> n; nrf = n; ad.resize(n + 1); sub.resize(n + 1); for(int i = 1; i < n; i++){ cin >> x >> y; ad[x].push_back(y); ad[y].push_back(x); } dfs(1, 0); nrf = sub[1]; cf = get_centroid(1, 0); vector <vector <int>> gramezi; for(auto fiu : ad[cf]){ vector <int> v; gramezi.push_back(v); Frunza(fiu, cf, gramezi.back()); } sort(gramezi.begin(), gramezi.end(), cmp); cout << (nrf + 1) / 2 << '\n'; if(nrf % 2 == 1){ cout << cf << ' ' << gramezi[0].back() << '\n'; gramezi[0].pop_back(); sort(gramezi.begin(), gramezi.end(), cmp); } while(gramezi[0].size() > 0){ cout << gramezi[0].back() << ' ' << gramezi[1].back() << '\n'; gramezi[0].pop_back(); gramezi[1].pop_back(); sort(gramezi.begin(), gramezi.end(), cmp); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...