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;
const int MAXN = 5e5 + 5;
int n;
vector <int> Leaves;
vector <int> Adj[MAXN];
void DFS(int node, int p = -1)
{
for(auto x : Adj[node])
{
if(x == p)
{
continue;
}
DFS(x, node);
}
if(Adj[node].size() == 1)
{
Leaves.push_back(node);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
Adj[u].push_back(v);
Adj[v].push_back(u);
}
DFS(1);
int s = Leaves.size();
cout << (int)(s + 1) / 2 << '\n';
for(int i = 0; i < (s + 1) / 2; i++)
{
cout << Leaves[i] << ' ' << Leaves[(i + (s + 1) / 2) % s] << '\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... |