Editorial for DMOPC '18 Contest 3 P4 - Bob and English Class
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the first subtask, it's optimal to connect the rightmost to the leftmost, second rightmost to the second leftmost, etc. A prefix sum can be used to speed this up.
Time Complexity:
For the second subtask, precompute the distance between each pair of students using BFS or DFS. Then, brute-force all possible configurations of pairs.
Time Complexity:
For the third subtask, root the tree arbitrarily. We think of the distances between pairs as each student moving up to their LCA. Now let be the total cost within the subtree of node such that students are still moving up. Now, we must be able to merge a node's children's values. Given a set of the children's 's, note if we can find the minimum for the node, we can obtain any larger with the same parity and within the total sum of the 's and the node's number of students. By considering this problem, we only need to consider the maximum , and the sum of the 's. So it can be done in polynomial time. The final answer is .
Time Complexity:
For the final subtask, we realize that the solution to the third subtask is absolutely awful. Root the tree at the centroid of the students (not the centroid of the entire tree!). Again, consider this problem. If we merge the children of the centroid with their maximum possible 's, then we can actually obtain , which means that all the students have been paired up. Also, since the children of the centroid have maximum possible 's, this must be the maximum possible total. So the optimal case is when all the students head to their centroid. This can be implemented in linear time.
Time Complexity:
Comments