제출 #529332

#제출 시각아이디문제언어결과실행 시간메모리
529332Alex_tz307Network (BOI15_net)C++17
0 / 100
7 ms12052 KiB
#include <bits/stdc++.h> using namespace std; const int kN = 5e5; vector<int> g[1 + kN], a; void dfs(int u, int par) { if ((int)g[u].size() == 1) { a.emplace_back(u); } for (int v : g[u]) { if (v != par) { dfs(v, u); } } } /* 9 1 2 2 3 2 4 2 5 1 6 1 7 3 8 3 9 */ void testCase() { int n; cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; g[u].emplace_back(v); g[v].emplace_back(u); } int root = -1; for (int v = 1; v <= n; ++v) { if ((int)g[v].size() > 1) { root = v; } } dfs(root, 0); cout << (a.size() + 1) / 2 << '\n'; for (int i = 0, j = a.size() - 1; i < j; ++i, --j) { cout << a[i] << ' ' << a[j] << '\n'; } if ((int)a.size() % 2 == 1) { cout << root << ' ' << a[a.size() / 2] << '\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int tests = 1; for (int tc = 0; tc < tests; ++tc) { testCase(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...