1414 */
1515
1616public class HeavyLightDecomposition {
17- private List <Integer >[] tree ;
17+ private List <List < Integer >> tree ;
1818 private int [] parent ;
1919 private int [] depth ;
2020 private int [] subtreeSize ;
@@ -24,11 +24,10 @@ public class HeavyLightDecomposition {
2424 private int [] segmentTree ;
2525 private int positionIndex ;
2626
27- @ SuppressWarnings ("unchecked" )
2827 public HeavyLightDecomposition (int n ) {
29- tree = new ArrayList [ n + 1 ] ;
28+ tree = new ArrayList <>() ;
3029 for (int i = 0 ; i <= n ; i ++) {
31- tree [ i ] = new ArrayList <>();
30+ tree . add ( new ArrayList <>() );
3231 }
3332 parent = new int [n + 1 ];
3433 depth = new int [n + 1 ];
@@ -52,14 +51,14 @@ public int getPositionIndex() {
5251 }
5352
5453 public void addEdge (int u , int v ) {
55- tree [ u ] .add (v );
56- tree [ v ] .add (u );
54+ tree . get ( u ) .add (v );
55+ tree . get ( v ) .add (u );
5756 }
5857
5958 private void dfsSize (int node , int parentNode ) {
6059 parent [node ] = parentNode ;
6160 subtreeSize [node ] = 1 ;
62- for (int child : tree [ node ] ) {
61+ for (int child : tree . get ( node ) ) {
6362 if (child != parentNode ) {
6463 depth [child ] = depth [node ] + 1 ;
6564 dfsSize (child , node );
@@ -73,7 +72,7 @@ private void decompose(int node, int head) {
7372 position [node ] = positionIndex ++;
7473 int heavyChild = -1 ;
7574 int maxSubtreeSize = -1 ;
76- for (int child : tree [ node ] ) {
75+ for (int child : tree . get ( node ) ) {
7776 if (child != parent [node ] && subtreeSize [child ] > maxSubtreeSize ) {
7877 heavyChild = child ;
7978 maxSubtreeSize = subtreeSize [child ];
@@ -82,7 +81,7 @@ private void decompose(int node, int head) {
8281 if (heavyChild != -1 ) {
8382 decompose (heavyChild , head );
8483 }
85- for (int child : tree [ node ] ) {
84+ for (int child : tree . get ( node ) ) {
8685 if (child != parent [node ] && child != heavyChild ) {
8786 decompose (child , child );
8887 }
0 commit comments