제출 #79559

#제출 시각아이디문제언어결과실행 시간메모리
79559IOrtroiiiNetwork (BOI15_net)C++14
100 / 100
687 ms41524 KiB
#include <bits/stdc++.h> using namespace std; const int N = 500005; int n; vector<int> g[N]; vector<int> leaves; void dfs(int u,int p) { bool leaf = 1; for (int v : g[u]) if (v != p) { dfs(v, u); leaf = 0; } if (leaf) leaves.push_back(u); } int main() { ios_base::sync_with_stdio(false); 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 = -1; for (int i = 1; i <= n; ++i) if (g[i].size() >= 2) { root = i; break; } dfs(root, -1); int sz = leaves.size(); cout << ((sz + 1) / 2) << '\n'; if (sz & 1) { cout << leaves[sz / 2] << ' ' << root << '\n'; for (int i = 0; i < (sz / 2); ++i) { cout << leaves[i] << ' ' << leaves[i + (sz / 2) + 1] << '\n'; } } else { for (int i = 0; i < (sz / 2); ++i) { cout << leaves[i] << ' ' << leaves[i + (sz / 2)] << '\n'; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...