forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CentroidDecomposition.java
69 lines (60 loc) · 2.18 KB
/
CentroidDecomposition.java
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
60
61
62
63
64
65
66
67
68
69
package structures;
import java.util.*;
import java.util.stream.Stream;
// https://sai16vicky.wordpress.com/2014/11/01/divide-and-conquer-on-trees-centroid-decomposition/
public class CentroidDecomposition {
static void calcSizes(List<Integer>[] tree, int[] size, boolean[] deleted, int u, int p) {
size[u] = 1;
for (int v : tree[u]) {
if (v == p || deleted[v])
continue;
calcSizes(tree, size, deleted, v, u);
size[u] += size[v];
}
}
static int findTreeCentroid(List<Integer>[] tree, int[] size, boolean[] deleted, int u, int p, int vertices) {
for (int v : tree[u]) {
if (v == p || deleted[v])
continue;
if (size[v] > vertices / 2) {
return findTreeCentroid(tree, size, deleted, v, u, vertices);
}
}
return u;
}
// static void dfs(List<Integer>[] tree, boolean[] deleted, int u, int p) {
// for (int v : tree[u]) {
// if (v == p || deleted[v]) continue;
// dfs(tree, deleted, v, u);
// }
// }
static void decompose(List<Integer>[] tree, int[] size, boolean[] deleted, int u, int total) {
calcSizes(tree, size, deleted, u, -1);
int centroid = findTreeCentroid(tree, size, deleted, u, -1, total);
deleted[centroid] = true;
// process centroid vertex here
// dfs(tree, deleted, centroid, -1);
System.out.println(centroid);
for (int v : tree[centroid]) {
if (deleted[v])
continue;
decompose(tree, size, deleted, v, size[v]);
}
}
public static void centroidDecomposition(List<Integer>[] tree) {
int n = tree.length;
decompose(tree, new int[n], new boolean[n], 0, n);
}
// Usage example
public static void main(String[] args) {
int n = 4;
List<Integer>[] tree = Stream.generate(ArrayList::new).limit(n).toArray(List[] ::new);
tree[3].add(0);
tree[0].add(3);
tree[3].add(1);
tree[1].add(3);
tree[3].add(2);
tree[2].add(3);
centroidDecomposition(tree);
}
}