Editorial for IOI '17 P3 - Toy Train
Submitting an official solution before solving the problem yourself is a bannable offence.
For a set of stations , define as the set of all stations such that if the train is placed on them, Arezou can play in a way that the train reaches one of the stations in at some point, regardless of Borzou's moves (therefore by definition). We define similarly.
Initially, let , and we iteratively add stations to such that eventually becomes equal to .
While there exists a station that satisfies one of the following conditions, we add to the set :
- is owned by Arezou, and there exists an outgoing track from that arrives at a station already in .
- is owned by Borzou, and all of the outgoing tracks from arrive at the stations already in .
Similarly, we can compute . Notice that both and can be computed iteratively in time .
Now let be the set of all the charging stations. By definition, for every station , Borzou can win the game if the train is initially placed on .
Therefore, we can solve the problem as follows:
- If is the set of all stations, Arezou can win the game for all initial stations.
- Otherwise:
- Let be the set of all stations not in .
- Borzou can win the game if the initial stations are in .
- Remove from the graph and solve the problem recursively for the remaining stations.
This algorithm runs in time .
Comments