제출 #96021

#제출 시각아이디문제언어결과실행 시간메모리
96021popovicirobertNetwork (BOI15_net)C++14
100 / 100
819 ms73436 KiB
#include <bits/stdc++.h> #define lsb(x) (x & (-x)) #define ll long long #define ull unsigned long long #define ld long double // 217 // 44 using namespace std; const int MAXN = (int) 5e5; vector <int> g[MAXN + 1]; int weight[MAXN + 1]; void dfs(int nod, int par) { weight[nod] = (g[nod].size() == 1); for(auto it : g[nod]) { if(it != par) { dfs(it, nod); weight[nod] += weight[it]; } } } int getCentroid(int nod, int par, int cnt) { for(auto it : g[nod]) { if(it != par && weight[it] > cnt / 2) { return getCentroid(it, nod, cnt); } } return nod; } vector < vector <int> > leaves; priority_queue < pair <int, int> > pq; int id[MAXN + 1]; void dfs1(int nod, int par, int cur) { int son = 0; for(auto it : g[nod]) { if(it != par) { dfs1(it, nod, cur); son++; } } if(son == 0) { leaves[cur].push_back(nod); id[nod] = cur; } } int main() { //ifstream cin("A.in"); //ofstream cout("A.out"); int i, n; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; for(i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } int root; for(i = 1; i <= n; i++) { if(g[i].size() > 1) { root = i; break; } } dfs(root, 0); root = getCentroid(root, 0, weight[root]); int sz = g[root].size(); leaves.resize(sz); for(i = 0; i < sz; i++) { dfs1(g[root][i], root, i); } for(i = 0; i < sz; i++) { pq.push({leaves[i].size(), i}); } vector < pair <int, int> > sol; int leaf = 0; while(pq.size() > 1) { int id1 = pq.top().second; pq.pop(); int id2 = pq.top().second; pq.pop(); sol.push_back({leaves[id1].back(), leaves[id2].back()}); if(leaf == 0) { leaf = leaves[id1].back(); } leaves[id1].pop_back(); leaves[id2].pop_back(); if(leaves[id1].size()) { pq.push({leaves[id1].size(), id1}); } if(leaves[id2].size()) { pq.push({leaves[id2].size(), id2}); } } if(pq.size()) { sol.push_back({leaf, leaves[pq.top().second].back()}); } cout << sol.size() << "\n"; for(auto it : sol) { cout << it.first << " " << it.second << "\n"; } //cin.close(); //cout.close(); return 0; }

컴파일 시 표준 에러 (stderr) 메시지

net.cpp: In function 'int main()':
net.cpp:74:10: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
     root = getCentroid(root, 0, weight[root]);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...