Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)

Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m arcs, loops are allowed. Gennady and Georgiy have a token placed in one of the graph vertices. Players take turns moving the token along one of the arcs that starts in the vertex the token is currently in. When there is no such arc, then this player loses the game. For each initial position of the token and the player who is moving first, your task is to determine what kind of result the game is going to have. Does it seem to be easy? Not so much. On one side, Gennady is having a lot of fun playing this game, so he wants to play as long as possible. He even prefers a strategy that leads to infinite game to a strategy that makes him a winner. But if he cannot make the game infinite, then he obviously prefers winning to losing. On the other side, Georgiy has a lot of other work, so he does not want to play the game infinitely. Georgiy wants to win the game, but if he cannot win, then he prefers losing game to making it infinite. Both players are playing optimally. Both players know preferences of the other player.
Input
In the first line there are two integers — the number of vertices n (1 ≤ n ≤ 100000) and the number of arcs m (1 ≤ m ≤ 200000). In the next m lines there are two integers a and b on each line, denoting an arc from vertex a to vertex b. Vertices are numbered from 1 to n. Each (a,b) tuple appears at most once.
Output
In the first line print n characters — i-th character should denote the result of the game if Gennady starts in vertex i. In the second line print n characters — i-th character should denote the result of the game if Georgiy starts in vertex i. The result of the game is denoted by “W” if the starting player wins the game, “L” if the starting player loses the game, and “D” (draw) if the game runs infinitely.

题意: 给定有向图,选定起点,每次可以沿着有向图走到下一个点,没有点走的人输。这样很常规的SG图就可以搞定,但是现在加入新规定,A选手如果可以走无限步也算赢。而B选手宁愿输也不愿意走无限步。 现在对于每个点作为起点,输出A做为先手的结果,以及B选手作为先手的结果。

思路: 有向图,而且有环,不能简单的拓扑 :首先我们想一下SG图的概念,如果后续节点有一个为输,那么当前结点就为赢;如果后续节点都为赢,那么当前节点就为输。所以我们先取入度为0的点加入队列,状态为输。如果队首元素导致前缀节点入度为0,则加入队列;同时,队首元素如果为输,那么显然前缀节点也加入队列,且状态为赢,不论其入度是否为0。  我们先通过这样的思路求出哪些是无限走(平局,这里是A赢)的点,然后求出必赢和必输点。

(自己WA了几发,感觉挖掘了新天地。

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9381202.html
今日推荐