Editorial for DMOPC '17 Contest 3 P6 - Mimi and Scarf


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: r3mark

For the first subtask, call DFS from each node i to find the longest valid path with i as an endpoint.

Time Complexity: \mathcal O(N^2)

For the second subtask, note that the answer will always be the diameter unless the entire tree is a path and this path is invalid. Finding the diameter can be done in two DFS's (or BFS's if you want). If the diameter is the same as the number of nodes in the tree, then the entire tree is a path. (Alternatively, you can check if a tree is a path by looking at the degrees of each node, although for this subtask, you have to find the diameter anyways). Perform a DFS from one of the endpoints of the entire path to see if the entire path is a stripe or not. If the tree is not a path, or this path is not a stripe, then the answer is equal to the diameter. Otherwise, it is the diameter minus one.

Time Complexity: \mathcal O(N)

For the third subtask, we use centroid decomposition to break down the tree. Let i be an arbitrary node and consider its subtree (obtained from centroid decomposition). To merge paths P_1 and P_2 with endpoints at i and in its subtree, there are two cases:

Case 1: The node directly after i in P_1 has a different colour than the node directly after i in P_2.

Then P_1 and P_2 can simply be merged. Define S[C] to be the maximum length of any path going through a neighbour with colour C. To find the maximum length, find the largest two elements in S and take their sum minus one.

Case 2: The node directly after i in P_1 has the same colour as the node directly after i in P_2.

Consider an arbitrary path P from i in its subtree. Call the length of the stripe beginning from i in this path L. Let C be the colour of the neighbour of i which P leads from.

Loop through each neighbour of i. Let j be the current one.

For each value of L, record the longest total length of a valid path going through j which obtained this value of L. Call this length T[L]. For each value of L and value of C, record the longest total length of a valid path which passes through a neighbour which was DFSed before j and such that this path obtained these values of L and C. Call this length M[C][L].

DFS from j to compute this T array. Do not update the M array. To merge paths of the same colour, for each length L, you must find the maximum of T[L]+M[c_j][K-L]. Once this is done, update M[c_j] with the values in the T array. To maintain M, you can either use an RMQ Fenwick tree, or you can sort the neighbours based on the sizes of their subtrees and maintain the updates in linear time. Either way yields the same time complexity.

The final answer is the maximum length obtained over both these cases and over all nodes i.

Time Complexity: \mathcal O(N \log^2 N)


Comments

There are no comments at the moment.