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>
#define maxn (int)(1e5+51)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define inf 2000000001
#define mod 1000000007 //998244353
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
vector<int>g[5*maxn];
vector<int>leaves;
void dfs(int x,int p){
int ok = 0;
for(auto v:g[x]){
if(v==p)continue;
ok = 1;
dfs(v,x);
}
if(!ok||(x==1&&sz(g[x])==1))leaves.pb(x);
}
int main(){_
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);
}
dfs(1,1);
int x = sz(leaves);
cout<<(x+1)/2<<endl;
for(int i=0;i+1<x;i+=2)cout<<leaves[i]<<" "<<leaves[i+1]<<endl;
if(x&1){
cout<<leaves[0]<<" "<<leaves[x-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... |