Submission #641295

#TimeUsernameProblemLanguageResultExecution timeMemory
641295ghostwriterNetwork (BOI15_net)C++14
100 / 100
543 ms94520 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include <debug.h> #endif #define st first #define nd second #define pb push_back #define pf push_front #define _pb pop_back #define _pf pop_front #define lb lower_bound #define ub upper_bound #define mtp make_tuple #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ldb; typedef pair<int, int> pi; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll; typedef string str; template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); } template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i)) #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i)) #define EACH(i, x) for (auto &(i) : (x)) #define WHILE while #define file "TEST" mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); } /* Tran The Bao CTL - Da Lat Cay ngay cay dem nhung deo duoc cong nhan */ const int N = 5e5 + 5; int n, root, cntl[N], cnt = 0; vi adj[N], a[N], leafs; vpi ans; multiset<pi, greater<pi> > s; void dfs(int u, int p) { EACH(v, adj[u]) { if (v == p) continue; dfs(v, u); cntl[u] += cntl[v]; } if (sz(adj[u]) == 1) { leafs.pb(u); cntl[u] = 1; } } int fct(int u, int p, int s) { EACH(v, adj[u]) { if (v == p || sz(adj[v]) == 1) continue; if (cntl[v] > s / 2) return fct(v, u, s); } return u; } void dfs1(int u, int p) { EACH(v, adj[u]) { if (v == p) continue; if (u == root) ++cnt; dfs1(v, u); } if (sz(adj[u]) == 1) a[cnt].pb(u); } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); // freopen(file".inp", "r", stdin); // freopen(file".out", "w", stdout); cin >> n; FOR(i, 1, n - 1) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } FOR(i, 1, n) if (sz(adj[i]) > 1) root = i; dfs(root, 0); root = fct(root, 0, cntl[root]); dfs1(root, 0); FOR(i, 1, cnt) s.insert({sz(a[i]), i}); WHILE(sz(s) > 1) { int u = (*s.begin()).nd, v = (*++s.begin()).nd; s.erase(s.begin()); s.erase(s.begin()); ans.pb({a[u].back(), a[v].back()}); a[u]._pb(); a[v]._pb(); if (!a[u].empty()) s.insert({sz(a[u]), u}); if (!a[v].empty()) s.insert({sz(a[v]), v}); } if (!s.empty()) { int u = a[(*s.begin()).nd].back(); ans.pb({u, u == leafs[0]? leafs[1] : leafs[0]}); } cout << sz(ans) << '\n'; EACH(i, ans) cout << i.st << ' ' << i.nd << '\n'; return 0; } /* 9 2 1 3 2 4 3 5 4 6 2 7 5 8 5 9 7 */

Compilation message (stderr)

net.cpp: In function 'void dfs(int, int)':
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:42:2: note: in expansion of macro 'EACH'
   42 |  EACH(v, adj[u]) {
      |  ^~~~
net.cpp: In function 'int fct(int, int, int)':
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:53:2: note: in expansion of macro 'EACH'
   53 |  EACH(v, adj[u]) {
      |  ^~~~
net.cpp: In function 'void dfs1(int, int)':
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:60:2: note: in expansion of macro 'EACH'
   60 |  EACH(v, adj[u]) {
      |  ^~~~
net.cpp: In function 'int main()':
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:72:5: note: in expansion of macro 'FOR'
   72 |     FOR(i, 1, n - 1) {
      |     ^~~
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:78:5: note: in expansion of macro 'FOR'
   78 |     FOR(i, 1, n)
      |     ^~~
net.cpp:24:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   24 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
net.cpp:84:5: note: in expansion of macro 'FOR'
   84 |     FOR(i, 1, cnt) s.insert({sz(a[i]), i});
      |     ^~~
net.cpp:26:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   26 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
net.cpp:100:5: note: in expansion of macro 'EACH'
  100 |     EACH(i, ans) cout << i.st << ' ' << i.nd << '\n';
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...