Submission #96091

#TimeUsernameProblemLanguageResultExecution timeMemory
96091andrei110901Network (BOI15_net)C++14
63 / 100
2054 ms38692 KiB
#include <bits/stdc++.h> using namespace std; vector <vector <int>> ad, gramezi; 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 int &lhs, const int &rhs){ return gramezi[lhs].size() > gramezi[rhs].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; ios::sync_with_stdio(false); 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 <int> pa; for(auto fiu : ad[cf]){ vector <int> v; pa.push_back(gramezi.size()); gramezi.push_back(v); Frunza(fiu, cf, gramezi.back()); } sort(pa.begin(), pa.end(), cmp); cout << (nrf + 1) / 2 << '\n'; if(nrf % 2 == 1){ cout << cf << ' ' << gramezi[pa[0]].back() << '\n'; gramezi[pa[0]].pop_back(); sort(pa.begin(), pa.end(), cmp); } while(gramezi[pa[0]].size() > 0){ cout << gramezi[pa[0]].back() << ' ' << gramezi[pa[1]].back() << '\n'; gramezi[pa[0]].pop_back(); gramezi[pa[1]].pop_back(); sort(pa.begin(), pa.end(), cmp); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...