-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaltic OI '15 P3 - Network.cpp
59 lines (48 loc) · 1.31 KB
/
Baltic OI '15 P3 - Network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
https://dmoj.ca/problem/btoi15p3
same logic as https://dmoj.ca/problem/cco07p6 except we don't need to compress the biconnected components
Strategy:
- connect all leaf nodes in pairs
- if we have an odd number of leaf nodes, just connect the single one to any other leaf
*/
#include <iostream>
#include <vector>
using namespace std;
vector<int> graph[500005];
vector<int> order;
void traversal(int cur, int prev) {
if (graph[cur].size() == 1) {
order.push_back(cur);
}
for (int adj : graph[cur]) {
if (adj == prev) {
continue;
}
traversal(adj, cur);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
traversal(1, -1);
int cnt = order.size();
cout << (cnt + 1) / 2 << "\n"; // number of pairs
if (cnt % 2 == 1) { // odd case, pair with any other leaf
cout << order[0] << " " << order[cnt - 1] << "\n";
cnt -= 1;
order.pop_back();
}
int offset = cnt / 2; // match first half with second half
for (int i = 0; i < cnt / 2; i++) {
cout << order[i] << " " << order[i + offset] << "\n";
}
return 0;
}