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 <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void dfs(int node, int father, vector< vector<int> > &edges, vector<int> &leafs) {
if (edges[node].size() == 1)
leafs.push_back(node);
for (auto &x : edges[node])
if (x != father)
dfs(x, node, edges, leafs);
}
int main() {
ios::sync_with_stdio(false);
int N; cin >> N;
vector< vector<int> > edges(N);
for (int i = 1; i < N; ++i) {
int x, y; cin >> x >> y;
edges[x - 1].push_back(y - 1);
edges[y - 1].push_back(x - 1);
}
vector<int> leafs;
for (int i = 0; i < N; ++i) {
if (edges[i].size() == 1)
continue;
dfs(i, -1, edges, leafs);
if (leafs.size() % 2)
leafs.push_back(i);
break;
}
cout << leafs.size() / 2 << "\n";
for (int i = 0; i < int(leafs.size()) / 2; ++i)
cout << leafs[i] + 1 << " " << leafs[i + leafs.size() / 2] + 1 << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |