Official

D - The Simple Game Editorial by en_translator


In this kind of game theory tasks, it is often effective to backtrack the winners from the final states.

Our goal is to analyze for each state whether Alice or Bob will always win. Let \(dp_{i, j}\) be a boolean value representing whether Alice will always win, in the state where Alice and Bob has made \(i\) moves in total, and the piece is currently at vertex \(j\). (DP stands for Dynamic Programming.)

We evaluate \(dp_{i, j}\) for \(i = 2K, 2K - 1, \ldots, 1, 0\) in order.

First, for \(i = 2K\), if \(S_j =\) A then \(dp_{i, j}\) is true, and if \(S_j = \) B then \(dp_{i, j}\) is false.

For \(i = 2K - 2, 2K - 4, \ldots, 2, 0\), we need consider the next operation Alice performs. If there is a vertex \(v\) such that \(dp_{i + 1, v}\) is true and there is an edge from vertex \(j\) to vertex \(v\), then \(dp_{i, j}\) is true, because Alice can win when the piece is at \(j\) after \(i\) moves by moving it to vertex \(v\). Otherwise, \(dp_{i, j}\) is false.

Similarly, for \(i = 2K - 2, 2K - 4, \ldots, 2, 0\), we consider the next operation Bob performs. If there is a vertex \(v\) such that \(dp_{i + 1, v}\) is false and there is an edge to vertex \(v\), then \(dp_{i, j}\) is true, and otherwise it is false.

The final answer depends on the value \(dp_{0, 1}\). Since the DP has \(O(NK)\) states, and the transitions can be processed in a total of \(O((N+M)K)\) time, this algorithm runs fast enough under appropriate implementation.

Sample code (The implementation differs for several points, such as storing the DP table in a one-dimensional array.)

posted:
last update: