# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
797485 | bonk | Network (BOI15_net) | C++14 | 375 ms | 56520 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 = 5e5 + 2;
vector<int>adj[N];
vector<pair<int, int>>t;
int timer = 0;
void dfs(int prev, int cur){
timer++;
bool leaf = true;
for(auto &nxt: adj[cur]){
if(prev == nxt) continue;
leaf = false;
dfs(cur, nxt);
}
if(leaf) t.emplace_back(timer, cur);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
for(int i = 0; i < n - 1; i++){
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i = 1; i <= n; i++){
if(adj[i].size() > 1){
dfs(-1, i);
break;
}
}
sort(t.begin(), t.end());
cout << (t.size() + 1)/2 << '\n';
int half = t.size()/2;
for(int i = 0; i < half; i++){
cout << t[i].second << " " << t[i + half].second << '\n';
}
if(half*2 != t.size()){
cout << t[0].second << " " << t.back().second << '\n';
}
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... |