This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int maxn = 5e5;
vector<int> adj[maxn], lef;
void dfs(int u, int par){
int cnt = 0;
for(int v : adj[u]) if(v - par){
cnt++, dfs(v, u);
}
if(!cnt) lef.push_back(u);
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
int n; cin>>n;
for(int i=0; i<n-1; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1; i<=n; i++) if(adj[i].size() > 1) {
dfs(i, 0); break;
}
int sz = int((lef.size()+1)/2);
cout<<sz<<endl;
for(int i=0; i<sz; i++) {
cout<<lef[i]<<" " << lef[i + sz/2]<<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... |