제출 #655925

#제출 시각아이디문제언어결과실행 시간메모리
655925BehradmNetwork (BOI15_net)C++17
100 / 100
412 ms40812 KiB
/*\ In The Name Of GOD * Beyrad :D \*/ #include "bits/stdc++.h" #define sz(x) ((int) (x).size()) using namespace std; const int N = 5e5 + 11; int st[N]; int T = 0; vector<int> g[N]; void dfs(int u, int p) { st[u] = T++; for (int v : g[u]) { if (v != p) dfs(v, u); } } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; --u, --v; g[u].push_back(v); g[v].push_back(u); } int rt = 0; for (int i = 0; i < n; i++) if (sz(g[i]) >= 2) rt = i; dfs(rt, -1); vector<pair<int, int>> a; for (int i = 0; i < n; i++) if (sz(g[i]) == 1) a.emplace_back(st[i], i); sort(a.begin(), a.end()); int k = sz(a); cout << (k + 1) / 2 << '\n'; for (int i = 0; i < k / 2; i++) cout << a[i].second + 1 << " " << a[i + (k + 1) / 2].second + 1 << '\n'; if (k % 2 == 1) cout << a[k / 2].second + 1 << " " << rt + 1 << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...