Submission #79592

#TimeUsernameProblemLanguageResultExecution timeMemory
79592EntityITNetwork (BOI15_net)C++14
100 / 100
977 ms80828 KiB
#include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second typedef pair<int, int> ii; const int N = 5e5 + 5; int n, child[N], nLeaf, cur, tmp, pter[N]; vector<int> gr[N], vec[N]; void prep (int u, int p) { for (int v : gr[u]) if (v != p) { prep(v, u); child[u] += child[v]; } child[u] += (gr[u].size() == 1); } int findCentroid (int u, int p) { for (int v : gr[u]) if (v != p) { if (child[v] >= (nLeaf + 1) / 2 ) return findCentroid(v, u); } return u; } void dfs (int u, int p) { for (int v : gr[u]) if (v != p) dfs(v, u); if (gr[u].size() == 1) vec[cur].pb(u); } int main () { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int _ = 1; _ < n; ++_) { int u, v; cin >> u >> v; gr[u].pb(v); gr[v].pb(u); } int root; for (int i = 1; i <= n; ++i) if (gr[i].size() ) root = i; for (int u = 1; u <= n; ++u) nLeaf += (gr[u].size() == 1); cout << (nLeaf + 1) / 2 << '\n'; prep(root, root); int cen = findCentroid(root, root); for (int u : gr[cen]) cur ++, dfs(u, cen); priority_queue<ii> pq; for (int i = 1; i <= cur; ++i) { pq.push(ii(vec[i].size() - pter[i], i) ); } while (pq.size() > 1) { ii tmp1 = pq.top(); pq.pop(); ii tmp2 = pq.top(); pq.pop(); cout << vec[tmp1.se][ pter[tmp1.se] ] << ' ' << vec[tmp2.se][ pter[tmp2.se] ] << '\n'; pter[tmp1.se] ++; pter[tmp2.se] ++; if (pter[tmp1.se] < vec[tmp1.se].size() ) pq.push(ii(vec[tmp1.se].size() - pter[tmp1.se], tmp1.se) ); if (pter[tmp2.se] < vec[tmp2.se].size() ) pq.push(ii(vec[tmp2.se].size() - pter[tmp2.se], tmp2.se) ); } if (pq.size() ) { int u = vec[pq.top().se][ pter[pq.top().se] ]; for (int v = 1; v <= n; ++v) if (gr[v].size() == 1 && v != u) { cout << v << ' ' << u << '\n'; break; } } return 0; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:62:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (pter[tmp1.se] < vec[tmp1.se].size() ) pq.push(ii(vec[tmp1.se].size() - pter[tmp1.se], tmp1.se) );
             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
net.cpp:63:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (pter[tmp2.se] < vec[tmp2.se].size() ) pq.push(ii(vec[tmp2.se].size() - pter[tmp2.se], tmp2.se) );
             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
net.cpp:47:38: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
     int cen = findCentroid(root, root);
                                      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...