Submission #24530

#TimeUsernameProblemLanguageResultExecution timeMemory
24530BruteforcemanNetwork (BOI15_net)C++11
100 / 100
1116 ms52880 KiB
#include "bits/stdc++.h" using namespace std; int deg[500010]; deque <int> nodes; vector <int> g[500010]; int sub[500010]; int parent[500010]; void leaf(int x, int par) { if(deg[x] == 1) sub[x] = 1; else sub[x] = 0; for(auto i : g[x]) { if(i - par) { leaf(i, x); sub[x] += sub[i]; } } } int centroid(int x, int par, int range) { for(auto i : g[x]) { if(i - par) { if(sub[i] > range) { return centroid(i, x, range); } } } return x; } void dfs(int x, int par) { parent[x] = par; if(deg[x] == 1) { nodes.push_back(x); } for(auto i : g[x]) { if(i - par) { dfs(i, x); } } } bool vis[500010]; int lca(int x, int y) { memset(vis, false, sizeof vis); while(x != 0) { vis[x] = true; x = parent[x]; } while(y != 0) { if(vis[y] == true) return y; y = parent[y]; } return 1; } int main(int argc, char const *argv[]) { int n; scanf("%d", &n); for(int i = 1; i < n; i++) { int p, q; scanf("%d %d", &p, &q); ++deg[p]; ++deg[q]; g[p].push_back(q); g[q].push_back(p); } int root = 0; for(int i = 1; i <= n; i++) { if(deg[i] != 1) { root = i; break; } } leaf(root, 0); int half = (sub[root] + 1) >> 1; int leave = sub[root]; int centre = centroid(root, 0, half); dfs(centre, 0); leaf(centre, 0); assert(deg[centre] != 1); for(auto i : g[centre]) { if(sub[i] > half) { assert(false); } } printf("%d\n", half); for(int i = 0; i + half < leave; i++) { printf("%d %d\n", nodes[i], nodes[i + half]); } if(leave & 1) { if(lca(nodes[half-1], nodes.back()) == centre) printf("%d %d\n", nodes[half-1], nodes.back()); else printf("%d %d\n", nodes[half-1], nodes.front()); } return 0; }

Compilation message (stderr)

net.cpp: In function 'int main(int, const char**)':
net.cpp:61:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
net.cpp:65:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &q);
                         ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...