Editorial for WC '17 Contest 3 S4 - Relevant Results


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.

The pages can be modelled as a directed graph, with an edge corresponding to each link. Let's start by breaking the graph down into its strongly connected components (SCCs), which can be done in \mathcal O(N+E) time using Kosaraju's algorithm (where E is the total number of edges). Let's then define an "important" SCC as one with no incoming edges from other SCCs. We can now observe that, in order to satisfy the i-th question, it's sufficient to increase the relevancy score of a single page in each important SCC to at least Q_i. Within each important SCC, each other page in it must be reachable from the chosen page, and each unimportant SCC must be reachable from at least one important SCC.

These insights suggest an \mathcal O(MN+E) algorithm in which the M questions are answered independently. For each question i, we can consider each important SCC. Within that SCC, we can compute the cost of raising each of its pages' relevancy scores to at least Q_i, and then take the cheapest one. The answers for all of the important SCCs can then be added together to yield the final answer.

However, the above approach is only efficient enough when M = 1. A significantly more complex approach is required for full marks. The idea is that each page p corresponds to a linear function f_p(x) of cost required to increase that page's relevancy score to a given score x. We'd like to construct a similar function g_s for each important SCC s, such that g_s(x) = \min\{f_p(x)\} over all pages p in SCC s. The function g_s corresponds to the lower convex hull of the individual pages' linear functions, and its set of segments can be assembled in linear time after sorting the pages by their C values (which correspond to their lines' slopes).

From there, the answer to the i-th question is equal to the sum of g_s(Q_i) over all important SCCs s. In order to compute these answers efficiently for all questions, we can perform a unified line sweep over all interesting relevancy scores in non-decreasing order, with events for scores which are being asked about as well as scores at which convex hull segments begin. During the line sweep, we'll need to maintain both the current sum of g_s function values, and the sum of their current segments' slopes. These values can then be updated as necessary and used to fill in the answers to questions as they're encountered.

The time complexity of the above algorithm is \mathcal O((N+M) \log(N+M) + E).


Comments

There are no comments at the moment.