Submission #1283624

#TimeUsernameProblemLanguageResultExecution timeMemory
1283624wedonttalkanymoreNetwork (BOI15_net)C++20
100 / 100
479 ms192200 KiB
#include <bits/stdc++.h> /* Phia ben kia dai duong cung chi co Bo cat nang niu bien thoi */ using namespace std; using ll = long long; #define int long long #define pii pair<ll, ll> #define fi first #define se second const ll N = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19; // trinh ko co phai di chep code huyvg huhu :( int n; vector <int> adj[N]; set <int> s[N]; vector <pii> path; int pos = 0; void dfs(int u, int par) { if (adj[u].size() == 1 && u != pos) { s[u].insert(u); // cout << u << '\n'; return; } vector <set <int> > child(adj[u].size()); int sum = 0; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v != par) { dfs(v, u); for (auto x : s[v]) { child[i].insert(x); sum++; } s[v].clear(); } } // cout << "at: " << u << ' ' << sum << '\n'; // for (int i = 0; i < child.size(); i++) { // cout << adj[u][i] << ' ' << child[i].size() << '\n'; // } if (sum % 2 == 0) { for (int i = 0; i < child.size(); i++) { if (child[i].size()) { s[u].insert(*child[i].begin()); child[i].erase(child[i].begin()); break; } } } // cout << adj[u].size() << '\n'; for (int i = 1; i < child.size(); i++) { // cout << adj[u][i - 1] << ' ' << adj[u][i] << ' ' << child[i].size() << '\n'; while(child[i].size() && child[i - 1].size()) { int x = *child[i].begin(); int y = *child[i - 1].begin(); path.emplace_back(x, y); // cout << "allr: " << x << ' ' << y << '\n'; child[i].erase(x); child[i - 1].erase(y); } for (auto x : child[i - 1]) child[i].insert(x); child[i - 1].clear(); } for (int i = 0; i < child.size(); i++) { for (auto x : child[i]) { // cout << "ins: " << u << ' ' << x << '\n'; s[u].insert(x); } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); if (fopen(".inp", "r")) { freopen(".inp", "r", stdin); freopen(".out", "w", stdout); } cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) { pos = i; break; } } // cout << pos << '\n'; dfs(pos, 0); // cout << s[pos].size() << '\n'; // for (auto [x, y] : path) cout << x << ' ' << y << '\n'; if (s[pos].size()) { vector <int> val; for (auto x : s[pos]) { val.push_back(x); // cout << "need: " << x << '\n'; } if (val.size() % 2 == 1) { for (int i = 1; i < val.size(); i += 2) path.emplace_back(val[i], val[i - 1]); path.emplace_back(val.back(), pos); } else { int sz = val.size(); for (int i = 1; i < val.size() - 2; i += 2) path.emplace_back(val[i], val[i - 1]); path.emplace_back(val[sz - 1], pos); path.emplace_back(val[sz - 2], pos); } } cout << path.size() << '\n'; for (auto [x, y] : path) cout << x << ' ' << y << '\n'; return 0; }

Compilation message (stderr)

net.cpp: In function 'int main()':
net.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
net.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...