Submission #390058

#TimeUsernameProblemLanguageResultExecution timeMemory
390058BTheroNetwork (BOI15_net)C++17
100 / 100
688 ms58172 KiB
// chrono::system_clock::now().time_since_epoch().count() #include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int MAXN = (int)5e5 + 5; vi adj[MAXN], leaf; int sub[MAXN]; int n, m; void dfs(int v, int pr = -1) { sub[v] = (sz(adj[v]) == 1); for (int to : adj[v]) { if (pr != to) { dfs(to, v); sub[v] += sub[to]; } } } int cen(int v, int lim, int pr = -1) { for (int to : adj[v]) { if (to != pr && sub[to] > lim / 2) { return cen(to, lim, v); } } return v; } void dfsCol(int v, int pr, int c) { if (sz(adj[v]) == 1) { leaf.pb(v); } for (int to : adj[v]) { if (to != pr) { dfsCol(to, v, c); } } } void solve() { cin >> n; rep (i, 1, n) { int u, v; cin >> u >> v; adj[u].pb(v); adj[v].pb(u); } dfs(1); int tot = sub[1]; int root = cen(1, tot); for (int to : adj[root]) { dfsCol(to, root, ++m); } cout << (tot + 1) / 2 << '\n'; if (tot % 2 == 0) { for (int i = 0; i < tot / 2; i++) { cout << leaf[i] << ' ' << leaf[i + tot / 2] << '\n'; } } else { for (int i = 0; i <= tot / 2; i++) { cout << leaf[i] << ' ' << leaf[i + tot / 2] << '\n'; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; for (int i = 1; i <= tt; ++i) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...