Editorial for IOI '11 P1 - Tropical Garden
Submitting an official solution before solving the problem yourself is a bannable offence.
In this task, we would like to compute the number of possible paths that could have led each group to the specified intersection , using the given number of steps
.
Notice that each path is completely determined by its initial intersection. Thus, to compute the number of possible paths, we only need to check whether each intersection, if used as an initial intersection, would bring the group to intersection after exactly
steps. As we need to check all
initial intersections for each of the
groups, an efficient algorithm for checking whether the group will be at intersection
after exactly
steps is needed, which will be discussed in the following sections.
Graph construction
This problem can be treated as a graph problem. A natural approach is to construct a graph containing the following information: for each intersection, where the group would move to. Since they may take only one of the two most beautiful trails, we will use two vertices to represent an intersection. Namely, for the
intersection, let
represent this intersection where the next chosen trail must be the most beautiful trail incident to it, and
represent this same intersection but where the next chosen trail must be the second most beautiful trail incident to it (or the most beautiful trail if no alternative is available). In other words,
represents the
intersection when the last taken trail is not the most beautiful trail incident to this intersection, and
represents this intersection when the last taken trail is the most beautiful one incident to this intersection.
Now for each vertex, we add an outgoing edge representing the most beautiful or second most beautiful trail, according to the conditions mentioned above. With this, will contain
vertices, and exactly one outgoing edge from each vertex.
The construction of the graph takes
running time by first creating
vertices, then scanning through the array
representing trails, and conditionally adding these edges to
under the described conditions.
An
solution
A simple way to check where the couple would arrive after steps is to simulate their path, for each intersection as an initial vertex. Since they always choose the most beautiful trail in the first steps, the corresponding starting vertices in
are
.
To simulate their walk, we simply start at some vertex , then follow the unique outgoing edge for that vertex, and repeat this process for
steps. Since the vertices corresponding to intersection
are
and
, then this path ends at this intersection if and only if after
steps, we stop at one of these vertices. That is, to find the number of possible paths, we simulate their walk for all possible initial vertices
, and count the number of starting vertices that end at
or
after
steps.
Clearly, this process takes total running time for each starting vertex. Since there are
possible starting vertices and
questions, this algorithm takes
running time, including graph construction. This running time is sufficient to fully solve subtask 1.
An
solution
As becomes large in subtask 2, we need a better way to simulate the algorithm mentioned in the previous section. Notice that the edges in
represent
-step traveling. To simulate faster, we will use the permutation-composition approach.
We first precompute the result of -step traveling from each vertex in
, where
, using a technique similar to successive squaring. Let
represents the vertex we arrive at after traveling from
for
steps. Then for
, we can compute
easily: If
, then the destination is specified in
; otherwise, we compose the two paths of length
using the formula
. In other words, traveling
steps from
is the same as traveling
steps from
, then from that vertex, continue for
more steps.
Then, notice that for each value of , we can decompose this number into sum of distinct, non-negative powers of two. Suppose that
where
for some positive integer
. Then the result of traveling
steps from
can be found by simply composing travelings of
that we have precomputed. Using this technique, therefore, we can compute the destination for each starting intersection in
running time. Note that since
, we only need to compute
for
.
This algorithm takes extra running time to compute the values of
, as each of them can be computed in constant time. Then, we can find the destination of each path in
. Thus, the total running time is
, which is sufficient to fully solve subtask 2.
An
solution
Let us consider a more general question of determining whether a path starting at vertex with length
ends at vertex
. Recall that each vertex in
has exactly one outgoing edge. So, from any initial vertex, by simply following these edges, we will eventually enter a cycle. Thus, if we start at
, exactly one of the following conditions are met:
- We never reach
.
- We reach
just once exactly after some number of steps
. In this case,
is reachable, but is not on any cycle.
- We reach
for the first time after some number of steps
, and enter it every
steps. In this case,
is reachable, and is on a cycle of size
.
For our purpose of solving the problem, can vary depending on our initial vertex; namely, it can be any of the vertices
for
. However,
can only be vertices
and
. Since
does not vary very much, it is easier to check whether
is on a cycle, and whether it is possible to reach
from
.
To solve this problem, we create the graph , which is the same as graph
with its edges reversed. Then, we perform a depth-first search on this graph starting at
. During this search, we keep track of the distance of each reachable vertex from
. This number is the distance from
to
in
; that is, the number of steps
that brings us from
to
for the first time. At the same time, if we reach some vertex with a departing edge to
, then we obtain the size of the cycle
, which is the distance from
to that vertex plus
.
Thus, whether the path in starting at
with length
ends at
can be determined as follows:
- If we cannot reach
in
, then this path in
cannot end at
.
- If we reach
in
after
steps, but
is not on a cycle, then this path in
ends at
if and only if
.
- If we reach
in
after
steps, and
is in a cycle of size
, then this path in
ends at
if and only if
for some non-negative integer
.
For our task, the path starting at the intersection with length
will end at intersection
if and only if the path in
starting at
with length
ends at
or
. Note that during the implementation, we do not need to create graph
, but we can create
directly. It is also convenient to first create an array for storing
for each vertex
. We then initialize
and
to
, and update them during the depth-first search. We perform the search twice, starting at
and
, respectively.
Since the depth-first search takes running time and each query can be checked in constant running time, this algorithm takes
running time in total, which is sufficient to obtain full marks for this task.
Comments