제출 #632577

#제출 시각아이디문제언어결과실행 시간메모리
632577stevancvNetwork (BOI15_net)C++14
100 / 100
579 ms77144 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define sp ' ' #define en '\n' #define smin(a, b) a = min(a, b) #define smax(a, b) a = max(a, b) using namespace std; const int N = 5e5 + 2; const int mod = 1e9 + 7; vector<int> g[N]; int szl[N]; vector<vector<int>> all; void Dfs(int s, int e) { for (int u : g[s]) { if (u != e) { Dfs(u, s); szl[s] += szl[u]; } } smax(szl[s], 1); } int Dfs1(int s, int e, int t) { for (int u : g[s]) { if (u == e) continue; if (2 * szl[u] > t) return Dfs1(u, s, t); } return s; } void Dfs2(int s, int e, int r) { if (g[s].size() == 1) all.back().push_back(s); for (int u : g[s]) { if (u == e) continue; if (s == r) all.push_back({}); Dfs2(u, s, r); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int root = 1; for (int i = 1; i <= n; i++) if (g[i].size() > g[root].size()) root = i; Dfs(root, 0); root = Dfs1(root, 0, szl[root]); Dfs2(root, 0, root); int sz = all.size(); priority_queue<pair<int, int>> pq; vector<array<int, 2>> ans; for (int i = 0; i < sz; i++) pq.push({all[i].size(), i}); int who = -1; while (pq.size() > 1) { int x = pq.top().second; pq.pop(); int y = pq.top().second; pq.pop(); who = all[x].back(); ans.push_back({all[x].back(), all[y].back()}); all[x].pop_back(); all[y].pop_back(); if (all[x].size() > 0) pq.push({all[x].size(), x}); if (all[y].size() > 0) pq.push({all[y].size(), y}); } if (pq.size() == 1) ans.push_back({who, all[pq.top().second].back()}); cout << ans.size() << en; for (int i = 0; i < ans.size(); i++) { cout << ans[i][0] << sp << ans[i][1] << en; } return 0; }

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

net.cpp: In function 'int main()':
net.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for (int i = 0; i < ans.size(); i++) {
      |                     ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...