-
Notifications
You must be signed in to change notification settings - Fork 522
/
centroid_decomposition.cpp
58 lines (49 loc) · 1.62 KB
/
centroid_decomposition.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
#include <bits/stdc++.h>
using namespace std;
void calc_sizes(const vector<vector<int>> &tree, vector<int> &size, vector<bool> &deleted, int u, int p) {
size[u] = 1;
for (int v : tree[u]) {
if (v == p || deleted[v])
continue;
calc_sizes(tree, size, deleted, v, u);
size[u] += size[v];
}
}
int find_tree_centroid(const vector<vector<int>> &tree, vector<int> &size, vector<bool> &deleted, int u, int p,
int vertices) {
for (int v : tree[u]) {
if (v == p || deleted[v])
continue;
if (size[v] > vertices / 2) {
return find_tree_centroid(tree, size, deleted, v, u, vertices);
}
}
return u;
}
// void dfs(const vector<vector<int>> &tree, vector<bool> &deleted, int u, int p) {
// for (int v: tree[u]) {
// if (v == p || deleted[v])continue;
// dfs(tree, deleted, v, u);
// }
// }
void decompose(const vector<vector<int>> &tree, vector<int> &size, vector<bool> &deleted, int u, int total) {
calc_sizes(tree, size, deleted, u, -1);
int centroid = find_tree_centroid(tree, size, deleted, u, -1, total);
deleted[centroid] = true;
// process centroid vertex here
// dfs(tree, deleted, centroid, -1);
cout << centroid << endl;
for (int v : tree[centroid]) {
if (deleted[v])
continue;
decompose(tree, size, deleted, v, size[v]);
}
}
// usage example
int main() {
vector<vector<int>> tree = {{3}, {3}, {3}, {0, 1, 2}};
int n = tree.size();
vector<int> size(n);
vector<bool> deleted(n);
decompose(tree, size, deleted, 0, n);
}