#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> ());
vector<int> p(n + 1);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> a;
for (int i = 1; i <= n; i++)
if ((int) g[i].size() == 1)
a.push_back(i);
cout << (((int) a.size() + 1) >> 1) << '\n';
vector<int> q;
int y = -1;
for (int i = 0; i < (int) a.size(); i++) {
if (a[i] == -1)
continue;
bool ok = 0;
for (int j = i + 1; j < (int) a.size(); j++) {
if (a[j] == -1)
continue;
if (g[a[i]][0] == g[a[j]][0])
continue;
cout << a[i] << " " << a[j] << '\n';
y = a[i];
a[i] = a[j] = -1;
ok = 1;
break;
}
if (!ok)
q.push_back(a[i]);
}
for (int i = 0; i < (int) q.size(); i += 2) {
if (i + 1 < (int) q.size())
cout << q[i] << " " << q[i + 1] << '\n';
else cout << q[i] << " " << (y != -1 ? y : q[i - 1]) << '\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... |