Dynamic Tree Divide and Conquer
Dynamic Tree Divide and Conquer
Dynamic Tree Divide and Conquer is used to solve tree path information statistics problems with vertex/edge weight updates.
Centroid Decomposition Tree
Recall the computation process of centroid decomposition.
For a node \(x\), the simple paths in its subtree fall into two categories: those passing through node \(x\), consisting of one or two paths starting from \(x\); and those not passing through \(x\), which are already contained within the subtrees of all its child nodes.
To compute simple paths within a subtree, we select a decomposition center \(rt\), calculate the information of paths in the subtree passing through this node, then for each child node, treat the connected component containing that node after removing \(rt\) as a subtree and recursively compute. The selected decomposition centers can form a tree structure called the centroid decomposition tree. We observe that the total size of the connected blocks represented by nodes at the same level in the centroid decomposition tree (i.e., the connected blocks with that node as the decomposition center) is \(O(n)\). This implies that the time complexity of centroid decomposition is related to the depth of the centroid decomposition tree. If the depth of the centroid decomposition tree is \(h\), then the complexity of centroid decomposition is \(O(nh)\).
It can be proven that when we select the centroid of the connected block as the decomposition center each time, the depth of the centroid decomposition tree is minimized, at \(O(\log n)\). Thus, we can statistically process information for \(O(n^2)\) paths on the tree within \(O(n\log n)\) time complexity.
Since the tree structure does not change during dynamic centroid decomposition, the structure of the centroid decomposition tree also remains unchanged.
Below is reference code for constructing the centroid decomposition tree:
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 | |
Implementing Updates
During queries and updates, we traverse up the centroid decomposition tree by jumping to ancestors and updating. Since the depth of the centroid decomposition tree is at most \(O(\log n)\), this ensures acceptable complexity.
During dynamic centroid decomposition, we need information such as the distance from a node to its ancestors in the centroid decomposition tree. Since a node has at most \(O(\log n)\) ancestors, we can precompute the depth \(dep[x]\) or use LCA while constructing the centroid decomposition tree to preprocess these distances or implement real-time queries. Note: The distance from a node to its ancestors in the centroid decomposition tree is not necessarily monotonic; it cannot be accumulated!
During dynamic centroid decomposition, a node's information might be counted multiple times in its ancestors' information in the centroid decomposition tree. We need to eliminate the effect of these duplicates. A common method is to record information for a connected block in two ways: one is its distance information to the decomposition center, and the other is its distance information to the father of the decomposition center in the centroid decomposition tree. This part will be demonstrated in the example problems.
Example 「ZJOI2007」Hide and Seek
Given a tree with \(n\) nodes, initially all nodes are black. Implement the following two operations:
- Toggle the color of a node (white to black, black to white);
-
Query the distance between the two farthest black nodes on the tree.
\(n\le 10^5,m\le 5\times 10^5\)
Construct the centroid decomposition tree. For each node \(x\), maintain two erasable heaps. \(dist[x]\) stores distance information from all black nodes in the connected block represented by \(x\) to \(x\). \(ch[x]\) stores distance information from all black nodes among \(x\)'s children in the centroid decomposition tree and itself to \(x\). Due to the greedy method for answering queries in this problem, and since two paths from the same subtree cannot form a complete path, we only insert its own value and the maximum value from each of its subtrees into this heap. We find that the sum of the two largest values in \(ch[x]\) (or all values if there are less than two) is the longest black endpoint path passing through node \(x\) when the decomposition center is \(x\). We can use an erasable heap \(ans\) to store the answers for all nodes; the maximum value in this heap is the desired answer.
We can maintain these erasable heaps \(dist[x], ch[x], ans\) according to the definitions above. When values in \(dist[x]\) change, we can also maintain \(ch[x]\) and \(ans\) in \(O(\log n)\) time complexity.
Now let's see how \(dist[x]\) changes when we toggle a node's color. When the node was originally black, we perform a deletion operation; when it was originally white, we perform an insertion operation.
Suppose we want to toggle the color of node \(x\). For all its ancestors \(u\), we insert or delete \(dist(x,u)\) in \(dist[u]\), and simultaneously maintain the values of \(ch[x]\) and \(ans\). Specifically, we need to insert or delete the value \(0\) in \(ch[x]\).
Reference code:
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用