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 fi first
#define se second
#define pb push_back
typedef pair<int, int> ii;
const int N = 5e5 + 10;
const int oo = 1e9;
int n, cnt;
int leaf[N];
bool visit[N];
vector<int> lef, rig, vi[N];
void dfs(int pre, int u) {
for(int v: vi[u]) if(v != pre)
dfs(u, v);
if(vi[u].size() == 1) leaf[++cnt] = u;
}
signed 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;
vi[u].pb(v); vi[v].pb(u);
}
dfs(1, 1);
cout << (cnt + 1) / 2 << '\n' ;
for(int i = 1; i <= cnt / 2; ++i) cout << leaf[i] << ' ' << leaf[i + cnt / 2] << '\n' ;
if(cnt % 2 == 1) cout << leaf[cnt] << ' ' << leaf[cnt - 1] << '\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... |