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>
#define int long long int
#define ff first
#define ss second
#define FT ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int maxn = 5e5 + 10;
vector <int> adj[maxn];
vector <int> leaf;
void dfs (int v , int g)
{
if (adj[v].size() == 1)
{
leaf.push_back(v);
}
for (auto u : adj[v])
{
if (u != g)
{
dfs (u , v);
}
}
}
signed main()
{
FT;
int n;
cin >> n;
for (int i = 1 ; i < n ; i++)
{
int a , b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1 ; i <= n ; i++)
{
if (adj[i].size() > 1)
{
dfs (i , 0);
break;
}
}
int k = leaf.size();
cout << (k + 1) / 2 << endl;
for (int i = 0 ; i < (k + 1) / 2 ; i++)
{
cout << leaf[i] << " " << leaf[i + k / 2] << endl;
}
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... |