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 pb push_back
const int N = 5e5+3;
int deg[N];
vector<int> g[N];
vector<int> ls;
void dfs(int v, int p){
if(g[v].size()==1)ls.pb(v);
for(int to : g[v]){
if(to!=p)dfs(to, v);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for(int i=1; i<n; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
deg[a]++;
deg[b]++;
}
int root=1;
for(int i=2; i<=n; i++){
if(deg[i]>1)root=i;
}
dfs(root, root);
int cnt=ls.size();
cout << (cnt+1)/2 << '\n';
for(int i=0; i<cnt/2; i++){
cout << ls[i] << " " << ls[i+cnt/2] << '\n';
}
if(cnt&1)cout << ls[0] << " " << ls.back() << '\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... |