# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
75935 | Vasiljko | Network (BOI15_net) | C++14 | 11 ms | 10916 KiB |
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;
const int N = 200000+10;
vector<int>leaf;
vector<int>v[N];
void dfs(int s,int p){
if(v[s].size()==1){
leaf.push_back(s);
}
for(int i=0;i<v[s].size();i++){
if(v[s][i]!=p){
dfs(v[s][i],s);
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int n,x,y;
cin>>n;
for(int i=0;i<n-1;i++){
cin>>x>>y;
v[x].push_back(y);
v[y].push_back(x);
}
if(n==1){
cout<<1<<endl<<1<<" "<<1;
return 0;
}
dfs(1,-1);
int k=(int)leaf.size();
if(k%2==0){
cout<<k/2<<"\n";
for(int i=0;i<k/2;i++){
cout<<leaf[i]<<" "<<leaf[i+k/2]<<"\n";
}
}else{
cout<<k/2+1<<"\n";
for(int i=0;i<k/2;i++){
cout<<leaf[i]<<" "<<leaf[i+k/2+1]<<"\n";
}
cout<<leaf[k/2]<<" "<<1;
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |