Editorial for IOI '18 P3 - Werewolf
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
You choose a vertex where you transform yourself from human form to wolf form.
For each choice of , you need to decide whether it is possible to travel from to in human form (i.e. only using vertices whose indices are ), and to decide whether it is possible to travel from to in wolf form (i.e. only using vertices whose indices are ).
The time complexity of this solution is .
Subtask 2
Determine the set of vertices you can visit from in human form, and determine the set of vertices you can visit from in wolf form.
Then check whether these two sets intersect.
The time complexity of this solution is .
Subtask 3
Let be the set of the vertices which are reachable from by passing only vertices with index at least . Similarly, let be the set of the vertices which are reachable from by passing only vertices with index at most . Note that forms a range on the line on which cities are located. This range can be efficiently computed using doubling or Segment tree. can be similarly computed. Then, we can answer the query by checking whether these two ranges intersect.
Subtask 4
We can construct a rooted tree so that forms a subtree. This can be done using adding vertices to a disjoint set union structure in the descending order of indices. Then, using Euler-Tour on this tree, we can obtain a sequence of vertices and every corresponds to a contiguous segment of this sequence. We can compute a similar sequence for . Then, we can answer the query by checking whether two segments for and share a vertex. This can be done by the sweep line algorithm with a segment tree. The time complexity of this solution is .
Comments