@@ -27,6 +27,9 @@ public class HeavyLightDecomposition {
27
27
@ SuppressWarnings ("unchecked" )
28
28
public HeavyLightDecomposition (int n ) {
29
29
tree = new ArrayList [n + 1 ];
30
+ for (int i = 0 ; i <= n ; i ++) {
31
+ tree [i ] = new ArrayList <>();
32
+ }
30
33
parent = new int [n + 1 ];
31
34
depth = new int [n + 1 ];
32
35
subtreeSize = new int [n + 1 ];
@@ -35,7 +38,6 @@ public HeavyLightDecomposition(int n) {
35
38
nodeValue = new int [n + 1 ];
36
39
segmentTree = new int [4 * (n + 1 )];
37
40
for (int i = 0 ; i <= n ; i ++) {
38
- tree [i ] = new ArrayList <>();
39
41
chainHead [i ] = -1 ;
40
42
}
41
43
positionIndex = 0 ;
@@ -89,7 +91,7 @@ private void decompose(int node, int head) {
89
91
90
92
private void buildSegmentTree (int node , int start , int end ) {
91
93
if (start == end ) {
92
- segmentTree [node ] = nodeValue [start ];
94
+ segmentTree [node ] = nodeValue [start ];
93
95
return ;
94
96
}
95
97
int mid = (start + end ) / 2 ;
@@ -149,7 +151,9 @@ public void initialize(int root, int[] values) {
149
151
dfsSize (root , -1 );
150
152
decompose (root , root );
151
153
for (int i = 0 ; i < values .length ; i ++) {
152
- nodeValue [position [i ]] = values [i ];
154
+ if (i < positionIndex ) {
155
+ nodeValue [position [i ]] = values [i ];
156
+ }
153
157
}
154
158
buildSegmentTree (1 , 0 , positionIndex - 1 );
155
159
}
0 commit comments