This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>> &E , vector<bool> &used, vector<int> &L, int v = 0){
if (E[v].size() == 1){
L.push_back(v);
}
for (int w : E[v]){
if (!used[w]){
used[w] = true;
dfs(E, used, L, w);
}
}
}
int main(){
int n;
cin >> n;
vector<vector<int>> E(n);
for (int i = 0; i < n - 1; i++){
int a, b;
cin >> a >> b;
a--;
b--;
E[a].push_back(b);
E[b].push_back(a);
}
vector<bool> used(n, false);
used[0] = true;
vector<int> L;
dfs(E, used, L);
int cnt = L.size();
int k = (cnt + 1) / 2;
cout << k << endl;
for (int i = 0; i < k; i++){
cout << L[i] + 1 << ' ' << L[(i + k) % cnt] + 1 << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |