E - Shiritori 解説 by en_translator
hint
Actually this problem can be interpreted as a graph problem. Consider each word as a directed graph connecting the vertex representing the first three characters to the vertex representing the last three characters.
Editorial
First, let us consider each word as a directed graph connecting the vertex representing the first three characters to the vertex representing the last three characters. Then, we obtain a graph of vertices and edges.
Then we rephrase the problem as follows.
> Given is a directed graph, one of whose vertices has a piece on it.
> Takahashi and Aoki take turns to choose a directed edge starting from the vertex that the piece are currently on, and move the piece to the endpoint of the edge.
> The player who are unable to make a move will lose.
> For every vertex, determine who will win if the game start with the piece initially on that vertex.
This problem can be solved by the algorithm called backward induction. First, let us define winning, losing, and drawing vertex.
- A vertex without any directed edge starting from it is a losing vertex.
- A vertex where every directed edge from it ends at a winning vertex is also a losing vertex.
- A vertex where any directed edge from it ends at a losing vertex is a winning vertex.
- If a vertex does not satisfy any of the three conditions above, then it is always a drawing vertex.
Every time the winning/losing of a vertex has been determined, we check every vertex connected with a directed edge pointed at the edge. If we check it every time naively, it will cost a total of time; however, one can implement with a to solve it within a total of time.
Sample code (C++)
投稿日時:
最終更新: