Editorial for WC '17 Contest 3 S4 - Relevant Results
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 time using Kosaraju's algorithm (where 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 -th question, it's sufficient to increase the relevancy score of a single page in each important SCC to at least . 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 algorithm in which the questions are answered independently. For each question , 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 , 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 . A significantly more complex approach is required for full marks. The idea is that each page corresponds to a linear function of cost required to increase that page's relevancy score to a given score . We'd like to construct a similar function for each important SCC , such that over all pages in SCC . The function 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 values (which correspond to their lines' slopes).
From there, the answer to the -th question is equal to the sum of over all important SCCs . 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 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 .
Comments