# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
923267 | Aiperiii | Network (BOI15_net) | C++14 | 4 ms | 13872 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>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=5e5+5;
vector <int> g[N];
int d[N];
void dfs(int v,int p){
for(auto to : g[v]){
if(to!=p){
d[to]=d[v]+1;
dfs(to,v);
}
}
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;cin>>n;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
int root=1;
for(int i=1;i<=n;i++){
if(g[i].size()==1){
root=i;
}
}
//cout<<root<<"\n";
dfs(root,0);
vector <pair <int,int> > v;
for(int i=1;i<=n;i++){
if(g[i].size()==1 && i!=root)v.pb({d[i],i});
}
sort(all(v));
//cout<<v.size()<<"\n";
cout<<v.size()/2+1<<"\n";
for(int i=0;i<v.size()-1;i+=2){
cout<<v[i].ss<<" "<<v[i+1].ss<<"\n";
}
cout<<v.back().ss<<" "<<root<<"\n";
}
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... |