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<iostream>
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
bool visited[1000010];
vector<vector<int>> adj;
vector<int> listovi;
void dfs(int x)
{
visited[x] = true;
if( adj[x].size()==1 )
listovi.pb(x);
for( auto u : adj[x]){
if(!visited[u])
dfs(u);
}
}
int main(){
int n,a,b;
cin>>n;
adj.resize(n);
for(int i=0;i<n-1;i++){
cin>>a>>b;
a--;
b--;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(0);
// cout << " stigo " <<endl;
int sz = listovi.size();
cout << (sz+ 1)/2 <<endl;
// cout << sz << endl;
for(int i=0;i<sz/2;i++ ){
cout << listovi[i]+1 <<" " << listovi[ sz-1-i ]+1<<endl;
}
if( sz%2==1 ){
cout << listovi[sz/2]+1 << " " << listovi[0]+1 << endl;
}
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... |