Editorial for COCI '21 Contest 4 #4 Parkovi
Submitting an official solution before solving the problem yourself is a bannable offence.
The first subtask can be solved by iterating over all choices of parks and calculating the distances to the remaining nodes.
In the second subtask, the desired node is precisely the tree center, which can be found as one of the two central nodes on the tree diameter. The diameter of the tree can be found in a standard way, by calling a DFS two times to find the furthest node to the furthest node to some starting node.
Solving the third and fourth subtasks required binary search. In the third subtask, one iteration of binary search is just a simpler case of the general case of a tree. Therefore, we'll just describe the general case.
We do a binary search on the answer. Fix some and now we need to determine if it's possible to place parks so that each node has at least one park at a distance . We'll solve a somewhat stronger problem - we'll calculate the smallest number of parks needed to cover the entire tree, and compare this number to . Intuitively, it is not beneficial to take the leaves, but to try to take nodes as far from them as possible, but to still cover them.
The algorithm is the following: we maintain a tree of still unprocessed nodes. In each step, we choose some leaf. Call it and let's say it's connected to a still unprocessed node . We'll update some information for and remove . Additionally, it's possible that one of or is declared a park. This process is repeated until no more nodes are left.
We will refer to the nodes that were already processed and removed from the tree as being outside. The unprocessed nodes are divided up into two categories:
- Nodes which have already been covered by someone from the outside. For such nodes, we maintain an array representing the distance to the nearest park from the outside. This means that toward the inside, we can still cover nodes up to a distance of .
- Nodes which have not yet been covered from the outside and for which we decided that they will be covered at some point in the future. For such nodes, we maintain representing how far inward can we place a park so that it still covers along with all the nodes we haven't yet covered and which depend on to be covered.
At each step, we remove a leaf and for its neighbour , we update or . When doing this, it's possible that the category in which belongs to changes. How to exactly update this information for depends on a few simple cases and is left as an exercise to the reader. Let's illustrate one such case. If and are both already covered from the outside, and the length of the edge between them is , then we set to . What remains is to determine when a node should be declared a park. We do this when a leaf is not covered from the outside and when . If there is a strict inequality, we place a park in , and otherwise we can place it in .
The correctness of this algorithm follows from the fact that the parks are being placed at the last possible moment, i.e. when we don't have a choice. The time complexity of this procedure is , making the total complexity .
Comments