# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
244153 | kimbj0709 | Network (BOI15_net) | C++14 | 669 ms | 48112 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;
#define maxn 500050
vector<vector<int> > adj(maxn);
vector<int> leaf;
void dfs(int node,int parent){
for(auto k:adj[node]){
if(k==parent){
continue;
}
dfs(k,node);
}
if(adj[node].size()==1){
leaf.push_back(node);
return;
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int no_of_input;
int input1,input2;
cin >> no_of_input;
for(int i=0;i<no_of_input-1;i++){
cin >> input1 >> input2;
adj[input1].push_back(input2);
adj[input2].push_back(input1);
}
if(no_of_input==2){
cout << 1 << "\n";
cout << "1 2";
return 0;
}
int root = -1;
for(int i=1;i<=no_of_input;i++){
if(adj[i].size()>=2){
root = i;
break;
}
}
//cout << root << "--\n";
dfs(root,-1);
leaf.push_back(root);
vector<pair<int,int> > ans;
for(int i=0;i<leaf.size()/2;i++){
ans.push_back({leaf[i],leaf[i+leaf.size()/2]});
}
cout << ans.size() << "\n";
for(auto k:ans){
cout << k.first << " " << k.second << "\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... |