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 rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define SZ(a) (int)a.size()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
const int mx = 5e5;
vi adj[mx+1], leaves;
int x;
void dfs (int u, int p) {
if (SZ(adj[u])==1) leaves.pb(u);
else x = u;
for (int v : adj[u]) {
if (v==p) continue;
dfs (v,u);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
rep (i,1,n-1) {
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs (1,-1);
int m = leaves.size();
cout << (m+1)/2 << "\n";
rep (i,0,m/2-1) {
cout << leaves[i] << " " << leaves[i+m/2] << "\n";
}
if (m%2) cout << leaves[m-1] << " " << leaves[m/2] << "\n";
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... |