Editorial for Yet Another Contest 9 P6 - Tribes
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Clearly, the kingdom can be modelled as a tree.
Subtask 1
If there is only one unique , then there must be one tribe, but this tribe could be at any node. Then, the answer is .
Otherwise, there must be two tribes. If either of the subgraphs induced by the nodes occupied by a particular tribe are disconnected, then the answer is . If not, then let and be the unique pair of two nodes occupied by tribes and respectively, such that and are adjacent. The answer is the number of pairs of nodes such that is occupied by tribe , is occupied by tribe , and . This can be computed by calculating the number of nodes occupied by tribe at each distance from , and the number of nodes occupied by tribe at each distance from .
Time complexity:
Subtask 2
Define a region as a maximal connected subgraph occupied by the same tribe. If there are multiple regions by the same tribe, the answer is . Our goal is to pick a headquarter node from each region such that for any two adjacent regions, the distances of headquarter nodes to the boundary of the regions differs by at most .
For nodes and in adjacent regions, with being the node in 's region closest to , and being the node in 's region closest to , define . Then we need to hold for all chosen headquarters in adjacent regions.
To avoid confusion, we will refer to the tree given in the input as the kingdom tree. We can generate a new tree, the region tree, by compressing regions in the kingdom tree into a single node. Root this region tree arbitrarily; let the root be . Denote as the set of children of region in the region tree.
Let be the number of kingdom states for the subtree of in the region tree, where is chosen as the headquarters for its region. Then
The answer is then .
Time complexity:
Subtask 3
We need to speed up this DP. Consider using the dp values of one region to update the dp values for its parent in the region tree. We need a data structure over a tree which efficiently supports the following updates:
- Given a node , a distance and a value , multiply the values for each node at a distance of from by the value .
This can be done efficiently by building a centroid tree over the parent region in the kingdom tree. We rely on the property that for any two nodes and , the LCA of and in the centroid tree lies on the path between and in the kingdom tree. Thus, the set of nodes at distance from exactly corresponds to the set of nodes , where is the distance between nodes in the kingdom tree, and LCA is defined in the centroid tree. To handle an update, we can therefore iterate over all ancestors of in the centroid tree (possibly ), and make a note to update all descendants of in the centroid tree such that (note: we additionally require that , but we will see how to handle this later). After all updates, for a given node , we can loop over all ancestors of in the centroid tree, and multiply all of the notes for node tagged with distance .
There are multiple things to be careful about:
- If , let be the child of in the centroid tree which is closest to . In order for to hold, we require not to lie on the path from to . In other words, we only want to update descendants of in the centroid tree such that and does not lie in 's subtree in the centroid tree.
- Because of the above case, depending on implementation, it may be easier to separately consider paths which only go up the centroid tree, paths which only go down the centroid tree, and paths which go up and then down the centroid tree.
- For each node in a region, we need to make sure its DP value is the product of aggregated values from all of the children in the region tree. To clarify, consider an example where a parent region has a large depth, and one of its child regions has a small depth. Then some of the nodes in the parent region far away from the child region will be too far away to be updated by DP values in the child region; however, we actually want to set the DP values of these nodes to . To fix this, in addition to storing the number of kingdom states for each DP value, we can also store the number of values whose product contributed to the number of kingdom states. If this number of values does not match the number of child regions, then we can set the DP value to .
- You need to find an approach which only performs multiplications, since we cannot do modular division. There are multiple such approaches, such as doing sweep(s), or using prefix and suffix product arrays, but exact details are left as an exercise to the reader.
Time complexity:
Bonus: there is an alternative solution which utilizes the linear merge trick to efficiently combine DP values of children, yielding an solution.
Comments