Submission #855239

#TimeUsernameProblemLanguageResultExecution timeMemory
855239NeroZeinNetwork (BOI15_net)C++17
0 / 100
2 ms13144 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif const int N = 5e5 + 5; int dep[N]; vector<int> g[N]; int dfs(int v, int p) { int ret = v; for (int u : g[v]) { if (u != p) { dep[u] = dep[v] + 1; int t = dfs(u, v); if (dep[t] > dep[ret]) { ret = t; } } } return ret; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for(int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int a = dfs(1, 1); dep[a] = 0; int b = dfs(a, a); assert(g[a].size() == 1 && g[b].size() == 1); vector<pair<int, int>> ans = {{a, b}}; int t = -1; for (int i = 1; i <= n; ++i) { if ((int) g[i].size() > 1 || i == a || i == b) { continue; } if (t == -1) { t = i; } else { ans.emplace_back(t, i); t = -1; } } if (t != -1) { ans.emplace_back(1, t); } cout << ans.size() << '\n'; for (auto [u, v] : ans) { cout << u << ' ' << v << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...