Codeforces 936B Sleepy Game BFS+DFS判环

题面

题目链接:传送门

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”.

题意:A,B两人玩游戏,B睡着了,A来操控棋子。A先走,B后走。若A能走到某处后,轮到B走但无路可走,则A赢了。若两人共走了 106 10 6 步后仍未分出胜负,则为平局。

思路

分析题意,能否使”A走完后B无处可走“,等同于找到一条路径,使这条路径长度为奇数(即有偶数个节点)。
记录两个标记:flag表示是否平局,win表示是否能赢。两者初始值都为false。
先bfs一遍,若搜到的节点没有可达到的点,且此时路径长度为奇数,则将win设为true。
若bfs的路径长已超过 106 10 6 ,则将flag设为true并返回。
bfs完,若flag和win均为false,则说明A无法赢,也无法找到一条路径使它的长度超过 106 10 6 ,此时再dfs一次,判断是否有环路,若有,则A可以平局。
若dfs后发现无环路,则A必输。

代码

#include
#include
#include
using namespace std;
int read() {
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9') {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9') {
        x=10*x+ch-'0';
        ch=getchar();
    }
    return x*f;
}
const int Size=100001;
vector<int> to[Size];       //vector大法好 存每个点所能到达的节点 
bool vis[Size][2];          //判重数组 
bool flag=false,win=false;  //两种标记 
struct node {
    int father,x,y,t;
    /*father:记录队列目前节点的父节点,递归输出时用*/
} w[Size*20];
int n,m,head,tail,last,pos;
void PushQueue(int fx,int x,int y,int t) {  //入队 
    tail++;
    w[tail].x=x;
    w[tail].y=y;
    w[tail].father=fx;
    w[tail].t=t;
    vis[x][y]=true;
}
void bfs(int x) {           //bfs判路径长 
    PushQueue(0,x,1,0);
    while(headint len=to[w[head].x].size();
        if(!len) {
            win=!w[head].y;
            last=head;
            if(w[head].t>1e6)   flag=true;
            if(win) return;
        }
        for(int i=len-1; i>=0; i--) {
            if(!vis[to[w[head].x][i]][!w[head].y]) {
                PushQueue(head,to[w[head].x][i],!w[head].y,w[head].t+1);
            }
        }
    }
}
void Out(int x) {           //若可以赢,则递归输出节点编号 
    if(x)   Out(w[x].father);
    if(x)   printf("%d ",w[x].x);
}
int go[Size];
bool dfs(int x) {           //dfs判环 
    go[x]=1;
    int len=to[x].size();
    for(int i=0; iif(go[to[x][i]]==1) {
            return true;
        } else if(!go[to[x][i]]) {
            if(dfs(to[x][i]))   return true;    //注意不要写成return dfs(to[x][i]) 
        } else {
            continue;
        }
    }
    go[x]=-1;
    return false;
}
int main() {
    n=read();
    m=read();
    for(int i=1; i<=n; i++) {
        int l=read(),tmp;
        while(l--) {
            tmp=read();
            to[i].push_back(tmp);
        }
    }
    int s=read();
    bfs(s);
    if(win) {
        puts("Win");
        Out(last);
    } else if(flag || dfs(s)) {
        puts("Draw");
    } else {
        puts("Lose");
    }
    return 0;
}

你可能感兴趣的:(Codeforces题目)