Codeforces - Sleepy Game

Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and m edges. 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 ≤ 105, 0 ≤ 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 ≤ n, ai, 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
inputCopy
5 6
2 2 3
2 4 5
1 4
1 5
0
1
outputCopy
Win
1 2 4 5
inputCopy
3 2
1 3
1 1
0
2
outputCopy
Lose
inputCopy
2 2
1 2
1 1
1
outputCopy
Draw


看似是利用sg函数去 或运算 递推。但是实际上这整个是一个人行动的,所以只要能用奇数步到达出度为0的点,就可以胜利。不然如果有环,我们至少可以保证不败。

扫描二维码关注公众号,回复: 8515331 查看本文章

所以我们判环 + 找奇数步路径即可。dp[x][0/1] 表示到x步数为 奇数或者偶数 。

要注意我们找环的时候,找完点,最后把vis置为2。不然会找错。


AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,dag[N],m,s,vis[N],flag,dp[N][2],iswin,pre[N][2];
vector<int> g[N];
void find_loop(int x){
    if(flag)    return ;    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        if(vis[g[x][i]]==1)    return void(flag=1);    
        if(!vis[g[x][i]])   find_loop(g[x][i]);
    }
    vis[x]=2;
}
void dfs(int x,int f){
    if(!dag[x]&&f)  iswin=x;
    if(iswin)   return ;    dp[x][f]=1;
    for(int i=0,v;i<g[x].size();i++){
        v=g[x][i];
        if(dp[v][f^1])  continue;   
        pre[v][f^1]=x;    dfs(v,f^1);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1,num,x;i<=n;i++){
        scanf("%d",&num);   dag[i]=num;
        while(num--)    scanf("%d",&x),g[i].push_back(x);
    }
    cin>>s;    find_loop(s);    dfs(s,0);
    if(iswin){
        int t=iswin,f=0;    puts("Win");    stack<int> res;
        while(t){res.push(t);    f^=1;   t=pre[t][f];}
        while(res.size())   printf("%d ",res.top()),res.pop();
    }else if(flag)  puts("Draw");
    else    puts("Lose");
    return 0;
}
发布了416 篇原创文章 · 获赞 228 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/103793342