# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1064044 | manhlinh1501 | Network (BOI15_net) | C++17 | 251 ms | 47824 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;
using i64 = long long;
const int MAXN = 5e5 + 5;
using pii = pair<int, int>;
int N;
vector<int> adj[MAXN];
vector<int> leaves;
void DFS(int u, int p) {
if(adj[u].size() == 1) {
leaves.emplace_back(u);
return void(0);
}
for(int v : adj[u]) {
if(v == p) continue;
DFS(v, u);
}
}
signed main() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int i = 1; i < N; i++) {
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
for(int i = 1; i <= N; i++) {
if(adj[i].size() > 1) {
DFS(i, 0);
break;
}
}
if(leaves.size() == 1) return cout << 0, 0;
int k = leaves.size() / 2;
cout << (leaves.size() + 1) / 2 << "\n";
for(int i = 0; i < k; i++)
cout << leaves[i] << " " << leaves[i + k] << "\n";
if(leaves.size() % 2)
cout << leaves.back() << " " << leaves[k] << "\n";
return (0 ^ 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... |