Submission #623571

#TimeUsernameProblemLanguageResultExecution timeMemory
623571MinaRagy06Network (BOI15_net)C++17
0 / 100
6 ms11996 KiB
#include <bits/stdc++.h> using namespace std; #define lesgooo ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define endl '\n' #define int long long vector<int> adj[500'005]; int dist[500'005]; void dfs(int i, int sum, int par) { dist[i] = sum; for (auto nxt : adj[i]) if (nxt != par) dfs(nxt, sum+1, i); } signed main() { lesgooo; int n, u, v, sz = 0; cin >> n; for (int i = 0; i < n-1; i++) cin >> u >> v, adj[--v].push_back(--u), adj[u].push_back(v); dfs(0, 0, -1); vector<pair<int, int>> a; for (int i = 0; i < n; i++) if (adj[i].size() == 1) sz++, a.push_back({dist[i], i}); sort(a.begin(), a.end()); cout << (sz+1)/2 << endl; int l = sz/2-1, r = a.size()-1; while (l >= 0) cout << a[l--].second+1 << " " << a[r--].second+1 << endl; if (sz&1) { int mx = 0, x = 0, h = a[r].second; dfs(h, 0, -1); for (int i = 0; i < n; i++) if (i != l && dist[i] >= mx) mx = dist[i], x =i; cout << h+1 << " " << x+1 << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...