Editorial for DMOPC '19 Contest 7 P3 - Tree Pruning
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the first subtask, it suffices to brute force all possible choices of nodes to cut. For each choice, check to see if the remaining nodes are connected and if their sum falls within the range .
Time complexity:
For the second subtask, we root the tree arbitrarily and we perform dynamic programming on the different subtrees. Our dp state records whether there exists a subtree of weight exactly rooted at for all in . The state transitions consist of iterating through all children of and adding their values to 's dp in the same way as knapsack dp is performed. Since each state is either or , we store it inside a bitset. This allows us to perform state transitions using bit operations in where is the bitset constant (usually ).
Time complexity:
For the final subtask, we first remove all nodes with weight as they clearly cannot be part of our answer. If there is a node with weight in the range , we can use it as our answer and we are done. Otherwise, all remaining nodes have weight . We can then run a BFS/DFS on each connected component and add nodes to our answer until the total weight is . If our current total is and , we know that since the weight of any single node is . Therefore, this algorithm will find an answer if the total weight of any connected component is . If this is not the case, the answer doesn't exist which shows that the algorithm is correct.
Time complexity:
Comments