Submission #634779

#TimeUsernameProblemLanguageResultExecution timeMemory
634779tvladm2009Network (BOI15_net)C++14
100 / 100
611 ms65464 KiB
#include <iostream> #include <vector> using namespace std; const int MAX_N = 5 * 1e5; vector<int> g[MAX_N + 1], leaves; int n; void dfs(int u, int p = -1) { for (int v : g[u]) { if (v != p) { dfs(v, u); } } if (g[u].size() == 1) { leaves.push_back(u); } } int main() { cin >> n; for (int i = 1; i <= n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int root = 0; for (int i = 1; i <= n; i++) { if (g[i].size() > 1) { root = i; } } dfs(root); cout << (leaves.size() + 1) / 2 << "\n"; int cnt = leaves.size() / 2; for (int i = 0; i < cnt; i++) { cout << leaves[i] << " " << leaves[i + cnt + leaves.size() % 2] << "\n"; } if (leaves.size() % 2 == 1) { cout << root << " " << leaves[cnt] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...