#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define int long long int
const int N = 2e1 + 10;
const int md = 1e9 + 7;
const int INF = 1e9;
int32_t main(int32_t argc, char *argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
int n;
cin >> n;
vector<vector<int>> g(n + 1, vector<int> ());
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
set<int> s;
for (int i = 1; i <= n; i++)
if ((int) g[i].size() == 1)
s.insert(i);
cout << (((int) s.size() + 1) >> 1) << '\n';
int x = (*s.begin());
queue<int> q;
q.push(x);
vector<bool> visited(n + 1);
int ans;
while (!q.empty()) {
auto u = q.front();
q.pop();
if ((int) g[u].size() == 1)
ans = u;
for (auto v : g[u])
if (!visited[v]) {
q.push(v);
visited[v] = 1;
}
}
cout << x << " " << ans << '\n';
s.erase(x), s.erase(ans);
while ((int) s.size() > 1) {
cout << *s.begin() << " " << *(--s.end()) << '\n';
s.erase(s.begin());
s.erase(--s.end());
}
if ((int) s.size() == 1) {
cout << *s.begin() << " " << x << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |