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;
int N;
vector<int> adjList[500001];
vector<int> leafNodes;
void DFS(int node, int parent) {
int numVertices = 0;
for (auto child: adjList[node]) {
numVertices++;
if (child != parent) {
DFS(child, node);
}
}
if (numVertices == 1) {
leafNodes.push_back(node);
}
}
int main() {
cin >> N;
for (int i = 0; i < N-1; i++) {
int u, v;
cin >> u >> v;
adjList[u].push_back(v);
adjList[v].push_back(u);
}
DFS(1, -1);
int M = (int)leafNodes.size();
cout << (M/2)+(M%2) << "\n";
for (int i = 1; i <= M; i += 2) {
cout << leafNodes[i] << " " << leafNodes[(i+1)%M] << "\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... |