cf 936B Sleepy Game

一 原题

B. Sleepy Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and medges. One of the vertices contains a chip. Initially the chip is located at vertex s. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't move the chip loses. If the game lasts for 106 turns the draw is announced.

Vasya was performing big laboratory work in "Spelling and parts of speech" at night before the game, so he fell asleep at the very beginning of the game. Petya decided to take the advantage of this situation and make both Petya's and Vasya's moves.

Your task is to help Petya find out if he can win the game or at least draw a tie.

Input

The first line of input contain two integers n and m — the number of vertices and the number of edges in the graph (2 ≤ n ≤ 1050 ≤ m ≤ 2·105).

The next n lines contain the information about edges of the graph. i-th line (1 ≤ i ≤ n) contains nonnegative integer ci — number of vertices such that there is an edge from i to these vertices and ci distinct integers ai, j — indices of these vertices (1 ≤ ai, j ≤ nai, j ≠ i).

It is guaranteed that the total sum of ci equals to m.

The next line contains index of vertex s — the initial position of the chip (1 ≤ s ≤ n).

Output

If Petya can win print «Win» in the first line. In the next line print numbers v1, v2, ..., vk (1 ≤ k ≤ 106) — 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 in the graph. There must be no possible move from vertex vk. The sequence should be such that Petya wins the game.

If Petya can't win but can draw a tie, print «Draw» in the only line. Otherwise print «Lose».

Examples
input
Copy
5 6
2 2 3
2 4 5
1 4
1 5
0
1
output
Win
1 2 4 5 
input
Copy
3 2
1 3
1 1
0
2
output
Lose
input
Copy
2 2
1 2
1 1
1
output
Draw
Note

In the first example the graph is the following:

Initially the chip is located at vertex 1. In the first move Petya moves the chip to vertex 2, after that he moves it to vertex 4 for Vasya. After that he moves to vertex 5. Now it is Vasya's turn and there is no possible move, so Petya wins.

In the second example the graph is the following:

Initially the chip is located at vertex 2. The only possible Petya's move is to go to vertex 1. After that he has to go to 3 for Vasya. Now it's Petya's turn but he has no possible move, so Petya loses.

In the third example the graph is the following:

Petya can't win, but he can move along the cycle, so the players will draw a tie.


二 分析

题意:甲乙两人在简单有向图(最多10^5个顶点,2*10^5条边)上玩一个游戏,开始时指定一个顶点作为起点,每一轮一个玩家从当前顶点出发,沿着一条边达到一个新的顶点,轮流进行,一个玩家无路可走时就算失败,进行10^6轮后仍未分胜负算平。你先手,并且可以指定后手玩家的操作,问是否可以必胜,如果不行,是否可以逼和。


分析:和局的充要条件是显然的:图中有从起点可达的环。获胜条件其实是找到一条长度为奇数的路径,终点是一个出度为0的点。把原图中每个点S拆分为S0和S1,如果原图中有一条边(S, T),则连接(S0, T1)和(S1, T0)。这样我们只要从起点S1做一次DFS,如果能找到出度为0的点T0,就找到了一条必胜路径。


三 代码

之后都按照这个代码风格来了 函数名首字母大写 变量全部小写 控制语句关键字(if/for/while)后加空格

/*
AUTHOR: maxkibble
LANG: c++ 17
PROB: cf 936B
*/

#include <cstdio>
#include <vector>
#include <queue>

using namespace std;

#define pb push_back

const int maxn = 1e5 + 10;

int n, m, d[maxn];
vector<int> g1[maxn], g2[maxn << 1], trace;
short vis[maxn << 1];

void Init() {
    scanf("%d%d", &n, &m);
    int c, t;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &c);
        while(c--) {
            scanf("%d", &t);
            g1[i].pb(t);
            d[t]++;
        }
    }
}

void BuildGraph() {
    for (int i = 1; i <= n; i++) {
        for (int t: g1[i]) {
            g2[i * 2 - 1].pb(t * 2);
            g2[i * 2].pb(t * 2 - 1);
        }
    }
}

bool Dfs1(int x) {
    if (vis[x]) return false;
    vis[x] = 1;
    trace.pb(x);
    if (x % 2 == 1 && g2[x].size() == 0) return true;
    for (int y: g2[x]) {
        if (Dfs1(y)) return true;
    }
    trace.pop_back();
    return false;
}

bool Dfs2(int x) {
    if (vis[x] == 2) return true;
    vis[x] = 2;
    for (int y: g1[x]) {
        if (Dfs2(y)) return true;
    }
    vis[x] = 1;
    return false;
}

int main() {
    Init();
    BuildGraph();
    
    int pos;
    scanf("%d", &pos);
    
    if (Dfs1(pos * 2)) {
        puts("Win");
        for(int item: trace) printf("%d ", (item + 1) >> 1);
        return 0;
    }
    
    if (Dfs2(pos)) puts("Draw");
    else puts("Lose");

    return 0;
}


猜你喜欢

转载自blog.csdn.net/max_kibble/article/details/79411501