We are given a directed graph with n vertices, m edges. There is a chip starting at s. Players alternate moves: Petya moves first, then Vasya, etc. If a player cannot move, they lose. If the game lasts for 10^6 turns, a draw is announced.
Vasya is asleep, so Petya will make moves for both players. So essentially Petya controls both sides. He wants to see if he can force a win (i.e., ensure that eventually Vasya cannot move on his turn) given that he controls both players? Actually careful: Petya is making both players' moves. So he can choose the moves for both sides, i.e., he controls the entire sequence. However, the win condition is: The player who cannot move loses. Since players alternate, it matters whose turn it is when there's no move. So if Petya can arrange a sequence such that at some point, it's Vasya's turn and there is no move, then Petya wins. Conversely, if eventually Petya has no move on his turn, then he loses (but he can avoid that by making different choices earlier? Actually he controls both, so he wants to win, so he will try to force Vasya into losing. But he could also get into a losing situation if he makes a mistake. He wants to know if there exists a strategy (i.e., a way to choose moves for both sides, consistent with turn order) that results in a win for Petya. Since he controls both, essentially he is asking if there exists a finite play (where moves alternate) that ends on Vasya's turn (i.e., after an even number of moves? Let's index turns: Turn 1: Petya moves, Turn 2: Vasya moves, Turn 3: Petya moves, etc. The game ends when the player whose turn it is has no moves. So if after k moves have been made, it's player's turn to move, but the current vertex has outdegree 0, then that player loses. So if the game ends after an even number of moves (i.e., Vasya's turn to move but cannot), then Petya wins. If it ends after an odd number of moves (Petya's turn to move but cannot), then Petya loses. Since Petya controls both, he can choose a path that ends on an even length (number of edges traversed) where the final vertex has outdegree 0, and ensure that along the way the moves are consistent with his control. There's no opponent with different objectives; he can force the game to follow any path he wants. But note: The game also declares a draw if it lasts for 10^6 turns. Actually careful: "If the game lasts for 10^6 turns the draw is announced." That means if the number of turns reaches 10^6 (i.e., moves made?) It says "turns", probably the number of moves (each player's turn counts as a turn). So if the game reaches 1e6 moves without a terminal state, it's a draw. So Petya could also try to force a draw by making the game go into a cycle or long enough to hit 1e6. But note that he can choose moves arbitrarily; he could try to loop indefinitely, but the draw is announced after exactly 1e6 turns, meaning that if he can make the game last at least 1e6 moves without anyone losing, then the outcome is a draw. However, since he controls both, he could also stop earlier if he wants a win or lose. But he would want the best outcome: win > draw > lose. So if there is any winning path (finite sequence ending on Vasya's turn with no move), he can achieve it. Otherwise, if there is no winning path, but he can avoid losing by ensuring that either the game loops forever or reaches 1e6 turns (draw), then he can get a draw. If all paths eventually lead to a loss for Petya (i.e., no matter what he does, if he continues, he will eventually be forced into a position where it's his turn and no move, or maybe he could avoid that by looping? But if there is any cycle, he could loop forever, thus avoiding loss and achieving draw. So the only way to lose is if from the start, there is no possibility to avoid eventually reaching a terminal vertex on his turn, and also he cannot loop indefinitely? Actually because he controls both, he can decide to take any outgoing edge at each step. So he can avoid stepping into a losing terminal if there is any alternative. However, the loss condition is when it's Petya's turn and there is no outgoing edge. So if at some vertex on Petya's turn, all outgoing edges lead to positions that eventually force a loss? But he controls both, so he can choose which edge to take. But the condition is: if the vertex has outdegree 0 on Petya's turn, then he loses immediately. So to avoid losing, he must ensure that whenever it's Petya's turn, the current vertex has at least one outgoing edge. He can then choose one. So if there is any infinite path (non-terminating) that avoids ever reaching a vertex with outdegree 0 on Petya's turn? Actually on Vasya's turn, he also controls, so he can also choose edges. But if it's Vasya's turn and the vertex has outdegree 0, then Vasya loses, which is a win for Petya. So that's desirable. So from Petya's perspective, he wants to either eventually force a vertex with outdegree 0 on Vasya's turn, or if impossible, at least keep playing forever (or up to 1e6) to get a draw, provided he can avoid being forced into a loss (i.e., avoid reaching a vertex with outdegree 0 on his turn). But if he can always avoid losing by choosing moves, and he cannot force a win, then he can get a draw.
Thus the problem reduces to: In a directed graph, with a starting vertex s, players alternate turns. Petya controls both. Determine if there exists a finite path (starting at s) such that:
- The length (number of edges) is even? Let's formalize: turns: after 0 moves, it's Petya's turn. After 1 move, it's Vasya's turn. After k moves, it's Petya's turn if k is even? Actually after k moves, the next player to move is: if k is even (0,2,4,...), it's Petya's turn; if k is odd, it's Vasya's turn. Because start: Petya's turn (0 moves made). After Petya moves, 1 move made, now Vasya's turn. So after k moves, the player to move is: Petya if k even, Vasya if k odd. The game ends when the player to move has no outgoing edges from the current vertex. So if after k moves, vertex v has outdegree 0, then if k even (Petya's turn), Petya loses; if k odd (Vasya's turn), Petya wins. So a winning path is a path v0 = s, v1, v2, ..., vk such that for each i < k, there is an edge from vi to vi+1, and outdegree(vk) = 0, and k is odd (so Vasya's turn). Also note that during the path, we don't care about intermediate outdegrees because we assume the game continues as long as there is a move; but if at some intermediate vertex the outdegree was zero, the game would have ended earlier. So for the path to be valid, it must not encounter a terminal vertex before the end. That means for all i < k, outdegree(vi) > 0. And outdegree(vk) = 0. And k odd. So Petya wins if there exists such a path.
If no such winning path exists, can Petya force a draw? A draw occurs if the game can be made to last at least 10^6 turns without either player losing. That essentially means there exists an infinite path (or at least a very long path) starting from s such that along the path, whenever it's Petya's turn (even indices), the current vertex has at least one outgoing edge (so he can continue), and at no point does a terminal vertex occur on Vasya's turn? Actually if a terminal vertex occurs on Vasya's turn, that's a win, not a draw. But if we are considering draw because no win is possible, we might still have a win? But we are in the case that win is impossible, so we are looking for either a draw or lose. However, if there is a win, we would have output Win. So in the draw scenario, we assume there is no winning path. But there might still be paths that would end on Vasya's turn (win) - but if there is any, we would have output Win. So in draw scenario, there is no winning path. Therefore any finite path that ends (i.e., reaches a dead-end) must end on an even length (Petya's turn) because if it ended on odd length, that would be a win. So all dead ends reachable without encountering earlier terminal? Actually if a path reaches a vertex with outdegree 0 at some step, the game stops there. So if that step is odd (Vasya's turn), it's a win. So if there is no win, then all terminal vertices are reachable only on even steps? But maybe some terminal vertices are reachable on both parities? The parity depends on path length. If a terminal vertex v (outdegree 0) is reachable from s via some path, we can consider the lengths of paths to v. If there exists any path to v with odd length (i.e., odd number of edges) such that all intermediate vertices have positive outdegree (so the game wouldn't have ended earlier), then that path yields a win. So "no winning path" means that for every terminal vertex v (sink), all paths from s to v (without passing through a sink earlier) have even length. Or maybe there is a path that reaches a sink at odd length but passes through another sink earlier? But if it passes through a sink earlier, the game would have ended at that earlier sink, so the later odd length cannot be realized because the game would have stopped. So effectively we need to consider only paths that don't hit a sink before the end.
But more importantly, to get a draw, Petya must be able to avoid losing indefinitely (or at least for 10^6 moves). That means he must have a strategy (a way to choose moves) that ensures the game never reaches a sink on his turn. Since he controls both, he can simply follow a path that never encounters a sink on his turn and also never ends? Actually if he can follow an infinite path (or a cycle) where all vertices on even positions (Petya's turns) have outdegree > 0, he can keep playing forever, resulting in a draw (or after 10^6 moves). However, note that if at some point on Vasya's turn the vertex is a sink, that would be a win, which would be better; but if no win exists, then such a scenario might be impossible? Actually it might be possible that there is a sink reachable on Vasya's turn (win) but we missed? We assume no win, so no such sink reachable on odd length without hitting earlier sink? But could there be a situation where there is a sink reachable on odd length but the path to it goes through a sink on even length, which would cause a loss earlier, preventing the win? That would mean that although there is a sink at odd depth, to get there you must pass through a sink on even depth, causing loss, so you cannot realize the win because the opponent (Petya) would have lost earlier. But since Petya controls both, he could avoid going through that losing vertex? He must choose a path; if the only way to reach that win-sink goes through a loss-sink (i.e., a vertex with outdegree 0 on even step), then he cannot avoid losing if he tries to go that way. So that doesn't give a win. So a win requires existence of a path that ends at a sink on odd step and all intermediate vertices have outdegree > 0. So indeed, we need to check that.
For a draw, we need to know if Petya can avoid losing indefinitely. That is, is there an infinite play (or a very long play) where Petya never gets stuck on his turn? Since he can control both, he can try to stay within a set of vertices where from any vertex that appears on his turn (even step), there is at least one outgoing edge, and also from vertices on Vasya's turn, there is at least one outgoing edge (otherwise it's a win, but we assume no win, so if there is a vertex on Vasya's turn with outdegree 0, that's a win, but then we would have output Win. However, if there is a vertex on Vasya's turn with outdegree 0 but all paths to it must go through a losing vertex for Petya? That could still be a win if Petya can avoid the losing vertex. Actually if there is a vertex v with outdegree 0 reachable on odd length without passing through a sink earlier, then it's a win. So if no win, then there is no such vertex reachable on odd length without passing through an even sink? But maybe there is a vertex v with outdegree 0 reachable on odd length, but all paths to it go through some vertex u that is a sink on even length. In that case, can Petya avoid u? Possibly there are multiple paths; if there exists any path to v that avoids any even sink, then it's a win. So if no win exists, then for every odd-length path to a sink, there is some even-length sink along the way (or the sink itself is even?). But more systematically, we can model it as a two-player game with Petya controlling both, but the turn order is fixed. Actually it's a deterministic control: Petya chooses all moves. So the set of possible plays is exactly the set of all finite or infinite paths starting from s that respect the graph. Petya can choose any such path. He wants to achieve a win if possible; else draw; else lose.
Thus the problem: Determine if there exists a finite path from s ending at a sink (outdegree 0) with odd length (number of edges) and with all intermediate vertices having outdegree > 0. If yes, output "Win" and such a path (any such path with length <= 10^6? Actually the path length could be large, but we need to output a sequence of vertices of length k+1? The problem says: print numbers v1, v2, ..., vk (1 ≤ k ≤ 10^6) — the sequence of vertices Petya should visit for the winning. Vertex v1 should coincide with s. For i=1... k-1 there should be an edge from vi to vi+1. There must be no possible move from vertex vk. So they output the vertices visited, starting at s and ending at the terminal vertex. The number of vertices in the output is k, where k is the number of moves? Actually they say "sequence of vertices Petya should visit". Typically v1 = s, v2 after first move, etc. So the length of the sequence equals number of moves + 1? Actually if there are k vertices printed, then the number of moves is k-1. They require that there is no move from vk. So the final vertex is a sink. And the condition for win is that after those moves, it's Vasya's turn to move, i.e., the number of moves is odd. Because after k-1 moves, the next player is: if (k-1) even -> Petya; odd -> Vasya. Since they want Vasya to be unable to move, (k-1) must be odd => k is even? Let's check: number of moves = k-1. For Vasya's turn, we need odd number of moves. So k-1 odd => k even. But the problem statement says "print numbers v1, v2, ..., vk (1 ≤ k ≤ 10^6)". They didn't specify parity. In the example: Input 1, output: Win \n 1 2 4 5. Here k=4, moves=3, which is odd. So indeed k-1 = 3 odd => Vasya's turn, win. So k must be even? Wait 4 is even, yes. So k is even. So they output an even number of vertices. So we need to produce a path of odd length ending at sink.
If no win, then we need to check if Petya can force a draw. That is, does there exist an infinite path (or at least a path of length >= 10^6) that avoids losing? Since we can output at most 10^6 vertices for win case, but for draw we just output "Draw". So we only need to decide.
If not win and not draw, then "Lose".
Now, the key is to analyze the graph to determine outcomes.
We can think of the game as a typical two-player impartial game but with both players controlled by one entity. However, we can reframe: Petya wants to find a path from s to a sink with odd length, with the condition that along the path, all vertices before the sink have outdegree > 0. This is like: is there a reachable sink via a path where the distance from s (in terms of edges) is odd and no vertex on the path before the sink is a sink? But if a vertex before the sink is a sink, then the game would stop earlier. So effectively, the path must not include any sink except at the end. So we can preprocess: remove all sinks (outdegree 0) as they are terminal. However, note that if we reach a sink on an odd step, win; on even step, loss. But if we reach a sink on an even step earlier, we lose immediately, so we would never proceed to later vertices. So any path that contains a sink before the final vertex is not a valid full game path because the game would end at the first sink encountered. So we can treat the game as: Starting from s, players move along edges until they hit a vertex with outdegree 0. The outcome depends on the parity of the number of moves taken to reach that sink. So the game is essentially a "shortest path" to any sink? Not exactly, because you might choose a longer path. But you can deliberately take longer routes to adjust parity.
Thus, the existence of a winning path is equivalent to: there exists a sink t reachable from s via a path where the length (number of edges) is odd, and along that path, all vertices except t have outdegree > 0 (which is true if we only consider vertices that are not sinks; but if the path goes through a sink that is not t, it would stop, so we need a path that does not go through any sink before t). So we need to consider the graph where we remove all sink vertices? But careful: if a vertex is a sink, it has outdegree 0, so from it there are no outgoing edges, so any path that includes it cannot continue beyond it. So if we consider a path from s to t that goes through another sink u != t, then the path is actually s -> ... -> u, and stops at u; we cannot reach t. So for a path to reach t, it must not contain any sink before t. So effectively we consider the subgraph induced by vertices that are not sinks, plus the sink t at the end. So t must be reachable from s without passing through any sink (including possibly other sinks) except t itself. That means from s we can travel through non-sink vertices and eventually step to t (which is a sink) directly. So t must be an out-neighbor of some non-sink vertex.
Thus, we can think of the graph as: vertices with outdegree 0 are terminal (sinks). For all other vertices, they have at least one outgoing edge. We are to determine if there is a path from s to some sink with odd length.
But note: It's possible that from s, all paths eventually lead to a sink, but all such sinks are at even distance? Then no odd-distance sink reachable without passing through sink? However, we might be able to take cycles to adjust parity. So the condition is: there exists a reachable sink t such that the distance (shortest path length) from s to t in the graph where we consider only non-sink vertices (except t) has some parity, but we can also take longer paths. Actually we need existence of any path (not necessarily shortest) from s to t that avoids other sinks, with odd length. This is equivalent to: In the subgraph of non-sink vertices (including possibly t? t is sink, but we can have edge from non-sink to t), we consider whether there exists a path from s to any sink t with odd length, where the path consists of vertices all non-sink except the final t. But note that if there is a cycle consisting only of non-sink vertices, we can extend the path arbitrarily. So the key is: If there exists any reachable sink from s (through non-sink vertices) and the distance (in terms of parity) can be made odd by taking cycles of odd length? More formally, consider the set of vertices that can reach a sink without passing through sinks? Actually any path from s that avoids sinks until the final step is constrained to stay in the set of vertices that are not sinks (call them "non-terminals"). Let's denote V0 = {v | outdegree(v) > 0}. Sinks: T = {v | outdegree(v) = 0}. For a sink t to be reachable, there must exist a path s = v0, v1, ..., vk = t such that v_i in V0 for i=0..k-1, and v_k in T. So essentially we need to consider the directed graph restricted to V0 plus edges from V0 to T. So we can ignore T except as final destinations.
Now, the existence of an odd-length path to some sink is: Does there exist t in T reachable from s via a path of odd length within V0∪{t}? Because we can go through V0 vertices arbitrarily, possibly repeating.
This is reminiscent of parity BFS or DFS: We want to know if there is a path from s to any sink with odd length. Since we can have cycles, if there exists any path from s to a sink, and also there exists a cycle of odd length reachable from s before reaching the sink, then we can adjust parity as needed? Wait, we need a path that ends at sink. If there is a path from s to a sink of length L (could be even or odd). If L is odd, then we have a winning path directly. If L is even, but there exists a cycle of odd length somewhere along the way (or reachable from s and then we can return to the same path), we might be able to change the parity. For example, suppose from s we can go to some vertex u, then take an odd cycle (back to u) and then continue to sink t. Then the total length becomes L' = (distance from s to u) + (odd cycle length) + (distance from u to t). Since adding an odd number changes parity, if the original distance from s to t via some path was even, we can make it odd by inserting an odd cycle. But we must ensure that while traversing the cycle, we never hit a sink. That means the cycle must consist entirely of non-sink vertices (since if it contained a sink, the game would end there). So if there is an odd cycle in the subgraph of non-sink vertices that is reachable from s and from which we can still reach a sink (i.e., the cycle is on a path to some sink), then we can adjust parity. Conversely, if all paths from s to any sink have even length, and there is no odd cycle reachable from s that can be used to flip parity, then all paths to sinks will have even length. But also, could there be a situation where there is a path to a sink that is odd, but the path goes through a vertex that is a sink earlier? That wouldn't be a valid full game path because the game would stop at the earlier sink. So we need to consider the possibility of reaching a sink at odd length without encountering an earlier sink.
Thus, to decide existence of a winning path, we need to check if there exists a reachable sink via a walk (allowing repeats) that has odd length and avoids sinks until the final step. This is equivalent to: In the directed graph, is there a reachable sink t such that there exists a path from s to t with odd length in the graph where we treat sinks as absorbing? But since sinks have no outgoing edges, any path that reaches a sink earlier would stop, so we can't "pass through" a sink. So we consider the graph where we delete all sink vertices? Actually we keep them but we cannot leave them. So effectively we want a walk that stays in V0 until the final step, then goes to T. So we can think of the graph induced by V0, plus edges from V0 to T. So we can treat T as special nodes.
Now, what about draw? A draw occurs if Petya can avoid losing indefinitely, i.e., he can keep the game going forever (or at least up to 10^6 moves) without ever reaching a sink on his turn. Since he controls both sides, he can try to follow an infinite path (or a long enough path) that never hits a sink. However, he also might want to avoid winning? No, if winning is possible, he would take it. So draw is only considered when win is impossible. But even if win is possible, draw could also be possible, but we output win. So in the draw analysis, we assume no winning path exists.
Now, if there is no winning path, can Petya force a draw? He will try to avoid losing. Losing occurs if the game ever reaches a sink on Petya's turn (even-length moves). So if he can keep the game going forever without ever encountering a sink on his turn, that yields draw. But note that if the game ever reaches a sink on Vasya's turn (odd length), that's a win, which is not possible under the assumption of no winning path? Actually it might be possible that there is a sink reachable on odd length, but all such paths go through a sink on even length earlier? That would mean that any attempt to reach that sink would cause a loss earlier, so you cannot realize that win. But maybe you could avoid that even sink by taking a different path? If there exists any path to an odd-sink that avoids even sinks, then that is a winning path. Since we assume no winning path, such a path does not exist. So any path that eventually reaches a sink will first encounter a sink on an even step? Or perhaps there is no path that avoids sinks entirely, meaning that any infinite path is impossible because from any reachable non-sink, eventually you must hit a sink? But you could cycle indefinitely if there is a cycle among non-sink vertices that does not lead to a sink? However, if a non-sink vertex has all outgoing edges leading eventually to sinks (maybe through other vertices), but you could still stay in the cycle if the cycle itself is a closed set with no exits to sinks? Actually if a vertex has an edge to a sink, that is an exit. But if you are in a cycle, you might have the choice to not take the exit. But if the cycle has no edges to sinks, then you can stay in the cycle forever. However, does the existence of a cycle guarantee that you can avoid sinks indefinitely? Yes, because you can simply keep following the cycle and never step to a sink. But we must consider turn parity: the vertices visited on Petya's turns are those at even indices (0,2,4,...). If the cycle length is odd or even, does that matter? Since you control both sides, you can decide which edge to take at each step. So if there is any cycle reachable from s without passing through a sink, you could potentially loop forever, as long as you never choose an edge that goes to a sink. But careful: At some vertex, you might be forced to go to a sink if all outgoing edges lead to sinks? But if a vertex has any outgoing edge to a non-sink, you can choose that edge and avoid sink. So for you to be able to stay in non-sink vertices forever, you need that from every vertex you might be at on your turn (even steps) and also on Vasya's turn? Actually you control both sides, so you can also choose edges on Vasya's turns. So you need to be able to avoid stepping into a sink at any step. However, if a vertex on Vasya's turn has only outgoing edges to sinks, then you would be forced to go to a sink, which would be a win if that sink is encountered on Vasya's turn (odd step). But that would be a win, which we assumed not possible. Actually wait: if from a vertex v (on Vasya's turn) all outgoing edges go to sinks, then if you are at v on Vasya's turn, you must move to a sink, and then on the next turn (Petya's turn) the sink has no moves, so Petya loses? Let's see: if it's Vasya's turn and you move to a sink, then after that move, the chip is at the sink. The number of moves made becomes even? Let's simulate: Suppose after k moves, it's Vasya's turn (k odd). At that vertex v, you (Petya controlling Vasya) must choose an edge. If all edges go to sinks, you choose one, moving to sink t. Now number of moves becomes k+1, which is even, so it's now Petya's turn. But t has outdegree 0, so Petya cannot move, and thus Petya loses. So moving to a sink on Vasya's turn results in loss for Petya! Because after moving to a sink, it's Petya's turn with no moves. That's losing. So we must be careful: The outcome depends on who is to move after arriving at the sink. If you move to a sink on Vasya's turn, then the next player (Petya) can't move => Petya loses. That's bad for Petya. So when controlling both, Petya wants to avoid moving into a sink on Vasya's turn if he wants to avoid loss. Actually earlier we said: win occurs when it's Vasya's turn and there is no move (i.e., sink on Vasya's turn). So if you move into a sink on Vasya's turn, that means after the move, it's Petya's turn at the sink, not Vasya's turn. Wait, check: The game ends when the player whose turn it is cannot move. So if it's Vasya's turn and the chip is at a sink (outdegree 0), then Vasya cannot move, so Vasya loses => Petya wins. That's the win condition. But what is the sequence? Suppose after k moves, it's Vasya's turn and the chip is at vertex v. If v is a sink, then Vasya cannot move, game ends immediately, and Petya wins. So you don't need to move to a sink on Vasya's turn; the win occurs if the chip is already at a sink on Vasya's turn. So how does the chip get to a sink on Vasya's turn? It could be that on the previous move (which was Petya's turn), Petya moved from some vertex to a sink. That would make the chip at sink after an odd number of moves? Let's see: Turn 0: Petya's turn, chip at s (non-sink). He moves to v1 (move 1). Now it's Vasya's turn, chip at v1. If v1 is a sink, then Vasya cannot move, so Petya wins. So winning path is: s -> v1 (sink) with one move (odd length). That's consistent: k=1 (odd) moves, final vertex sink, Vasya's turn to move but cannot. So win occurs when the sink is reached after an odd number of moves. Therefore, moving to a sink on Vasya's turn is not what happens; moving to a sink on Petya's turn results in the chip being at sink on Vasya's turn. Conversely, moving to a sink on Vasya's turn results in the chip at sink on Petya's turn, causing Petya to lose. So we must be careful about the parity of the move that arrives at sink.
Thus, to win, Petya wants to end on a sink after an odd number of moves. That means the last move is made by Petya (since odd number of moves: move 1 by Petya, move 2 by Vasya, ..., move odd by Petya). Actually number of moves: 1st move by Petya, 2nd by Vasya, 3rd by Petya, etc. So odd-numbered moves are by Petya, even-numbered by Vasya. After an odd number of moves, it's Vasya's turn (since after 1 move, Vasya's turn; after 3 moves, Vasya's turn). So the chip arrives at the sink after an odd move (by Petya), then Vasya cannot move -> win. So the final move is made by Petya from a non-sink to a sink.
Thus, for a winning path, we need an odd-length path from s to a sink, with the property that all vertices before the final sink are non-sinks (so the game doesn't end earlier). And the last move is from a non-sink to a sink.
Now, for loss: If the chip ever arrives at a sink after an even number of moves (i.e., after Vasya moves), then it's Petya's turn at a sink, Petya loses. Also, if at any point it's Petya's turn at a sink, loss occurs immediately. That could happen either because the chip started at a sink? Starting vertex s could be a sink. If s is a sink, then at turn 0 (Petya's turn), Petya cannot move, so he loses immediately. So lose if s is a sink? Actually then he loses. So we need to check that.
Now, for draw: Petya wants to avoid both win (if possible) and loss? Actually he wants win > draw > lose. So if win is possible, he'll take it. If not, he wants to avoid loss and try to draw. So if he cannot force a win, can he force a draw? That means he can ensure that the game never reaches a sink on Petya's turn, and also never reaches a sink on Vasya's turn? Wait, if he reaches a sink on Vasya's turn, that's a win, which would be good, but we assumed win is not possible. So if win is not possible, there is no way to reach a sink on Vasya's turn without earlier loss? Actually if win is not possible, that means there is no path to a sink with odd length without encountering a sink earlier? But could there be a way to reach a sink on Vasya's turn but with some earlier sink? The game would stop at the earlier sink. So to achieve win, you need a path that ends at a sink on Vasya's turn without hitting any sink before. So if no such path exists, it might still be possible to reach a sink on Vasya's turn but after having passed through some sink on an even step? That would have caused loss earlier, so you wouldn't get to that win. So effectively, if no winning path exists, then any play that eventually ends (i.e., reaches a sink) will end on an even step (Petya's turn) because the first sink encountered will be at some step; if that step were odd, that would be a winning path (since you haven't encountered a sink before). So the first sink encountered will be at an even step. So if the game eventually reaches a sink, it will be a loss for Petya. Thus, to avoid loss, Petya must avoid reaching any sink at all (or at least ensure that the game continues indefinitely without ever hitting a sink). But could he reach a sink on an odd step after having visited some sink earlier? No, because the game would have ended at the first sink. So indeed, if there is no winning path, any terminal play results in loss. Therefore, to achieve a draw, Petya must avoid ever reaching a sink. That means he must keep the chip moving forever (or up to 10^6 moves) without ever stepping onto a sink. Since he controls both players, he can try to stay within the set of vertices that are not sinks, and also ensure that he never is forced to move to a sink because all outgoing edges go to sinks. But if at some vertex (on either player's turn) all outgoing edges lead to sinks, then if it's his turn (Petya's turn), he would be forced to move to a sink, resulting in loss (since after moving to sink, it becomes Vasya's turn? Actually check: If it's Petya's turn at a non-sink vertex, and all outgoing edges go to sinks, then any move he makes goes to a sink. After the move, the chip is at a sink, and the number of moves becomes odd? Let's see: Suppose it's Petya's turn after an even number of moves (k even). He moves to a sink -> now moves = k+1 (odd). So it's Vasya's turn at a sink -> win! Wait, that would be a win for Petya! That contradicts earlier reasoning. Let's analyze carefully:
- Turn: Starting with 0 moves, it's Petya's turn.
- Petya moves: move count 1, now Vasya's turn.
- If Petya moves to a sink on his turn, then after the move, it's Vasya's turn at a sink -> Vasya cannot move -> Petya wins. So moving to a sink on Petya's turn yields a win, provided that the sink is reached exactly after an odd number of moves.
But earlier we said win occurs when the chip is at a sink on Vasya's turn. That's equivalent to the chip arriving at the sink after an odd number of moves (Petya's move). So indeed, if on Petya's turn at a non-sink, he can move to a sink, that's a winning move (provided the game hasn't ended earlier). So why would that be a problem? If from a vertex on Petya's turn all outgoing edges lead to sinks, then Petya can win by moving to a sink. That would be a winning path, which we assumed doesn't exist. So if no winning path exists, it must be that for every vertex reachable (with appropriate parity constraints) on Petya's turn, there is at least one outgoing edge to a non-sink? Not exactly; the existence of a winning path is about a path from s to a sink with odd length and all intermediate vertices non-sink. That includes the possibility that at some vertex on Petya's turn, there is a direct edge to a sink. That would be a winning move. So if from any vertex reachable on Petya's turn with an odd number of moves from start (i.e., at even distances? Let's define parity of distance from s in terms of number of moves taken to reach that vertex. At the start, distance 0 (Petya's turn). After odd number of moves, it's Vasya's turn; after even number, it's Petya's turn. So vertices reachable after an even number of moves are positions where it's Petya's turn; after odd number, Vasya's turn. A winning move is: at a vertex v reached after an even number of moves (Petya's turn), there is an edge to a sink. Then moving to that sink yields win. So if such a vertex v is reachable without hitting a sink earlier, that's a win.
Thus, if no winning path exists, then there is no vertex v reachable from s via a path that stays in non-sink vertices (except possibly the final move) such that distance from s to v is even and there is an edge from v to a sink. Also, there is no longer odd-length path to a sink that avoids sinks before.
But also, consider vertices reached on Vasya's turn (odd distance). If at such a vertex, all outgoing edges go to sinks, then moving to a sink would be a losing move? Let's see: If it's Vasya's turn at vertex v (odd distance), and v has only sink out-neighbors, then whatever move Vasya (controlled by Petya) makes, the chip goes to a sink. After the move, moves become even, so it's Petya's turn at a sink -> Petya loses. So that would be a losing move. Since Petya controls Vasya, he would avoid moving to a sink in that situation if possible. But if all outgoing edges are to sinks, then he is forced to move to a sink, resulting in loss. So to avoid loss, he must avoid being at such a vertex on Vasya's turn, or ensure there is an alternative edge to a non-sink. So for a draw, Petya must be able to avoid ever being forced to move to a sink on Vasya's turn, and also avoid being forced to move to a sink on Petya's turn? Actually on Petya's turn, moving to a sink is winning, so he would do it if possible. But if no win exists, then on Petya's turn from any reachable vertex, either there is no direct edge to a sink, or if there is, that vertex might not be reachable without hitting a sink earlier? But we assume no winning path, so even if there is an edge from a reachable Petya-turn vertex to a sink, maybe that vertex is not reachable because to reach it you must go through a sink earlier? That seems contradictory because if it's reachable, then you could just go there and then move to sink, yielding win. So for no win, such vertices must not be reachable without hitting a sink earlier. That means any path from s to such a vertex must pass through a sink (on some turn) before. That sink would end the game earlier, perhaps on an even turn causing loss, preventing you from getting to that vertex. So indeed, the existence of cycles might allow you to avoid earlier sinks? But if all paths to that vertex go through a sink, then you cannot reach it without ending the game earlier. So from a draw perspective, if there is a cycle that avoids sinks entirely, you could stay in that cycle forever and avoid ever hitting a sink. That would yield a draw.
But also, even if there is no such cycle, it's possible that from s, all paths eventually lead to a sink, and the first sink encountered will be at an even distance (since win is not possible). Then any play will result in loss. So that would be a lose.
Thus, the problem reduces to determining, in a directed graph with some vertices being sinks (outdegree=0), the outcome for the starting vertex s under optimal control by Petya (who chooses all moves). This is similar to solving a game where each vertex has a state (win/lose/draw) based on the usual two-player impartial game with alternating turns, but here both players are controlled by the same agent. However, the agent wants to maximize outcome: win > draw > lose. But since he controls both, he can force the best outcome among all possible plays. This is essentially a "one-player game" where the player chooses both moves but must respect turn order. This is similar to finding if there exists a path to a sink with odd length (win), else if there exists an infinite path avoiding sinks (draw), else lose.
We need to compute the set of vertices from which Petya can force a win, or can force a draw, etc. But careful: Since Petya chooses all moves, the game is deterministic from his choices. So we can think of it as a graph where at each step, the player whose turn it is (Petya or Vasya) selects an outgoing edge. However, since Petya makes both moves, it's not a typical two-player adversarial game; it's more like a puzzle where the controller wants to achieve a certain outcome. So we need to compute whether there exists a strategy (a mapping from game history to moves) that results in win, draw, or lose. But we can simplify: Since Petya controls both sides, the sequence of vertices is entirely under his control, subject to the constraint that the moves must follow outgoing edges. So essentially it's just a path (possibly infinite) chosen by Petya. There is no adversarial choice; it's just a choice of which outgoing edge to take at each step. So the existence of a winning path is simply existence of a finite path ending at a sink with odd length, as described. The existence of a draw is existence of an infinite path that never visits a sink (since visiting a sink would end the game, and if that sink is visited on an odd length, it's win; if on even length, it's lose; but we assume no win path exists, so if we ever hit a sink, it would be on even length (loss) because if it were on odd length, that would be a win path, contradicting assumption). So to avoid loss, we must avoid hitting any sink at all. So draw exists iff there is an infinite path (or a cycle) from s that never visits a sink. However, note that it's possible that any infinite path would eventually hit a sink, but you could also prolong the game arbitrarily by taking longer paths, but if all paths eventually lead to a sink, you cannot avoid it forever. But could you avoid sinks for at least 10^6 moves? The problem statement says if the game lasts for 10^6 turns, a draw is announced. So even if you cannot avoid a sink indefinitely, you might be able to delay the sink beyond 10^6 moves. But the problem seems to treat "draw" as a possibility if the game can be prolonged to 10^6 turns without a terminal state. However, typical competitive programming problems of this type (like Codeforces problem "Game on a Directed Graph" or similar) treat draw as the existence of a cycle reachable from start that does not contain a winning or losing terminal states. Usually they set the draw if there is a cycle that can be repeated to make the game arbitrarily long, which would allow reaching 10^6 turns. So we can treat draw as existence of a reachable cycle (in the graph of non-sink vertices) from which you can stay forever, i.e., there is a cycle in the subgraph of vertices that are not sinks and that are reachable from s without passing through sinks? But careful: Even if there is a cycle, it might be that from s you are forced to eventually go into a sink because at some vertex on the cycle, all outgoing edges might go to sinks? Actually if a vertex is part of a cycle, it must have an outgoing edge that stays in the cycle (since it's a directed cycle). So it has at least one outgoing edge to another vertex in the cycle, which is not a sink. So you can always choose that edge to stay in the cycle. However, if at some vertex on the cycle there is also an edge to a sink, you can choose not to take it. So as long as you can reach a cycle composed of non-sink vertices, you can loop forever. So draw is possible if there exists a cycle in the subgraph of non-sink vertices that is reachable from s without visiting a sink. But we must also consider the parity? The parity of the turn when you are on the cycle might affect whether you can stay in the cycle indefinitely. But since you control both players, you can always choose the outgoing edges regardless of whose turn it is. So as long as there is a cycle reachable, you can follow it forever, regardless of turn parity. However, one nuance: If you are on a vertex that is part of a cycle, but on a particular turn (say Vasya's turn) the only outgoing edges from that vertex go to sinks, then you would be forced to go to a sink, causing loss (if on Vasya's turn) or win (if on Petya's turn). But if it's part of a cycle, there must be at least one outgoing edge to another vertex in the cycle, so you have a choice to stay in the cycle. So it's fine. So draw exists iff there is a reachable cycle in the graph induced by vertices with outdegree > 0 (non-sinks), i.e., there exists a cycle reachable from s without passing through sinks. But is that always true? Let's consider: Suppose from s, you can go to a set of vertices that eventually all lead to sinks, but there might be no cycle. Then any path is finite and ends at a sink. Since win is not possible, all such paths end at a sink on an even step (first sink encountered is on even step). Then Petya loses. So output Lose.
But what about the possibility that there is a cycle, but all paths from s to the cycle go through a sink? Then you cannot reach the cycle without hitting a sink first, which would end the game. So the cycle must be reachable without visiting a sink. So we need to consider the subgraph G' consisting of vertices with outdegree > 0. Consider the set of vertices reachable from s in G' (i.e., there exists a path from s to v using only vertices in G'). Note that we can also use edges from a vertex in G' to a sink, but if we go to a sink, the game ends. So for draw, we need an infinite path staying in G'. So we need that from s, there is a path that stays in G' forever (i.e., there is a reachable cycle in G'). Equivalent to: In the subgraph G', the start vertex s is in a component that contains a cycle. More precisely, if from s you can reach a vertex that is part of a cycle (in G'), then you can go to that vertex and then loop the cycle forever. But you also need to be able to reach that vertex without stepping into a sink. Since we are staying in G', that's exactly reachable in G'.
Thus, the problem becomes:
-
Determine if there exists a path from s to a sink t such that the length of the path (number of edges) is odd, and all vertices on the path except t are in G' (non-sinks). If yes, output Win and such a path.
-
Else, determine if there exists a cycle in G' reachable from s (i.e., s can reach a vertex that is part of a cycle in G'). If yes, output Draw.
-
Else, output Lose.
But is this sufficient? Consider the possibility where there is a cycle in G', but also there is a winning path. Since win takes precedence, we output Win. So draw only if no win. Also consider the possibility where there is a cycle, but maybe the cycle leads to a win? Actually cycle itself doesn't produce a win, but you could also combine cycle with a win path? If win exists, we already output win. So fine.
Now we must also consider the case where s itself is a sink. Then immediate outcome: It's Petya's turn and he cannot move -> Lose. Check: if s is sink, outdegree 0, then Petya loses. So output Lose. That fits: no win (since any path to sink with odd length would require at least one move, but you can't move), no draw (since game ends immediately). So output Lose.
Now, what about the scenario where there is no cycle, but there is an infinite path due to an infinite graph (like an infinite chain)? But graph is finite, so any infinite path must contain a cycle. So only cycles allow infinite play.
Now, the nuance: The problem says "If the game lasts for 106 turns the draw is announced." So even if there is no cycle but there is a very long path (>=1e6) before hitting a sink, you could achieve draw by simply taking that long path. However, the problem expects that if you can't win but you can avoid losing for at least 1e6 turns, you output Draw. In many Codeforces problems like this (e.g., "Petya and Vasya and Game" or similar), the limit 1e6 is just to indicate that if you can loop indefinitely, you can always reach that many moves. But if the graph is finite and has no cycles, the maximum length of any path from s without revisiting a vertex is bounded by n. Since n <= 1e5, the longest simple path is at most n-1, which is less than 1e6. So if there is no cycle, the game will end (with loss) in at most n moves. So the only way to reach 1e6 moves is to have a cycle. So it's sufficient to check for cycles.
But wait, could there be a path longer than 1e6 without cycles? Since number of vertices is at most 1e5, the longest simple path is at most 1e5-1. That's < 1e6. So no. So indeed, draw is equivalent to existence of a cycle reachable from s without passing through sinks.
Thus, we need to compute:
-
Determine if there exists a path from s to a sink of odd length, staying in non-sink vertices until the last step.
-
If not, determine if there is a cycle in the subgraph of non-sink vertices reachable from s.
-
Otherwise, Lose.
Now, we also need to output a winning path if exists, with length up to 1e6. The path length can be at most 1e6. Since n <= 1e5, we could potentially output a path that includes cycles to adjust parity, which might be long. But we are limited to 1e6. So we need to construct a path that satisfies the condition. Typically, we can find a path to a sink of odd length, possibly by using cycles to adjust parity if needed.
But our condition for win is existence of any odd-length path from s to a sink (through non-sinks). This includes paths that may repeat vertices (cycles). However, if there is a reachable sink and also a cycle of odd length reachable from s before the sink, we can adjust parity. But is it necessary to consider cycles? Suppose there is a sink reachable only via even length, but there is an odd cycle reachable from s. Can we combine them to get an odd-length path to a sink? Let's formalize: Let the set of vertices from which a sink is reachable (via any path staying in non-sinks until the final step) be R. For each such vertex v, we can consider the parity of the shortest distance from s to v (assuming we avoid sinks) and also whether we can change parity by cycles. Existence of an odd-length walk to a sink is equivalent to there being a reachable sink with some walk length odd. Since cycles can be used, if there exists any path from s to a sink (avoiding sinks before) and also there exists any cycle of odd length reachable from s and from which the sink is still reachable (i.e., you can go from s to cycle, then from cycle to sink), then you can adjust parity. More formally, if there is any vertex u that is reachable from s without passing through sinks, and from u there is both a path to a sink and a cycle (reachable from u) of odd length, then you can get an odd walk to the sink by traversing the cycle an odd number of times to flip parity. But note that the cycle itself must consist of non-sink vertices (since if it contains a sink, you would stop). So the condition for existence of an odd-length walk to a sink is: there exists a sink t such that in the graph of non-sink vertices, the distance from s to t (in terms of walks) can be made odd. This is equivalent to: either there is a direct path of odd length, or there is a path of even length but there exists an odd cycle somewhere on the way (or more generally, there exists a vertex from which both a path to a sink and an odd cycle are reachable). Actually we need to be able to go from