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;
#define ll long long
#define pb push_back
const int maxn = 5e5 + 20;
vector<int> adj[maxn] , tmp;
void dfs(int v , int p = -1)
{
if((int)adj[v].size() == 1)
tmp.pb(v);
for(auto u : adj[v])
if(u != p)
dfs(u , v);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 0; i < n - 1; i++)
{
int a , b;
cin >> a >> b;
a-- , b--;
adj[a].pb(b);
adj[b].pb(a);
}
int root = -1;
for(int i = 0; i < n; i++)
if((int)adj[i].size() != 1)
{
root = i;
dfs(i);
break;
}
int k = tmp.size();
cout << (k + 1) / 2 << endl;
if(k & 1)
cout << root + 1 << " " << tmp.back() + 1 << endl , tmp.pop_back();
for(int i = 0; i < k / 2; i++)
cout << tmp[i] + 1 << " " << tmp[i + k / 2] + 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... |