CF 936B Sleepy Game(判环+BFS)

题目链接:http://codeforces.com/problemset/problem/936/B

题目:

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 ≤ 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 ≤ 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 62 2 32 4 51 41 501

output

Win1 2 4 5 

input

Copy

3 21 31 102

output

Lose

input

Copy

2 21 21 11

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.

题解:先手以自己最优选择,即选一个路线,奇数条边且最后一个点出度为0,则Win;如果形成环,Draw,否则Lose。path[u][t]表示当前顶点u能否通过奇数条(t=1)或偶数条(t=0)到达。BFS,最后再判环。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define eps 1e-8
 8
 9 const int N=1e5+10;
10 typedef long long LL;
11 vector <int> E[N];
12 int out[N],f[N];
13 queue < pair<int,int> > Q;
14 int path[N][2],ok;
15
16 void print(int u,int t){
17     if(path[u][t]!=-1) print(path[u][t],t^1);
18     printf("%d ",u);
19 }
20
21 void dfs(int u){
22     if(ok) return ;
23     f[u]=-1;
24     for(int i=0;i<E[u].size();i++){
25         int v=E[u][i];
26         if(f[v]==-1) {ok=1;return ;}
27         else dfs(v);
28     }
29     f[u]=0;
30 }
31
32 int main(){
33     int n,m,s;
34     scanf("%d%d",&n,&m);
35     for(int i=1;i<=n;i++){
36         int t,x;
37         scanf("%d",&t);
38         out[i]=t;
39         for(int j=1;j<=t;j++){
40             scanf("%d",&x);
41             E[i].push_back(x);
42         }
43     }
44     scanf("%d",&s);
45     pair <int,int> p,now;
46     p.first=s;p.second=0;
47     path[s][0]=-1;
48     Q.push(p);
49     while(!Q.empty()){
50         now=Q.front();Q.pop();
51         int t1=now.second^1;
52         for(int i=0;i<E[now.first].size();i++){
53             int v=E[now.first][i];
54             if(path[v][t1]==0){
55                 path[v][t1]=now.first;
56                 p.first=v;p.second=t1;
57                 Q.push(p);
58             }
59         }
60     }
61     for(int i=1;i<=n;i++){
62         if(!out[i]&&path[i][1]){
63             printf("Win\n");
64             print(i,1);
65             return 0;
66         }
67     }
68     dfs(s);
69     if(ok) printf("Draw\n");
70     else printf("Lose\n");
71     return 0;
72 }

原文地址:https://www.cnblogs.com/Leonard-/p/8495515.html

时间: 2024-10-12 02:39:46

CF 936B Sleepy Game(判环+BFS)的相关文章

CodeForces 937D 936B Sleepy Game 有向图判环,拆点,DFS

题意: 一种游戏,2个人轮流控制棋子在一块有向图上移动,每次移动一条边,不能移动的人为输,无限循环则为平局,棋子初始位置为$S$ 现在有一个人可以同时控制两个玩家,问是否能使得第一个人必胜,并输出一个解,否则判断是否能平局 题解: 看到这个题首先我想到了强连通分量,但是事实证明求出强连通分量,缩点对解决问题没有什么帮助.... 能写一些看似正确的算法,但其实是假算法来的.. ........... 所以应该先分析策略,肯定是能赢就赢,不能赢就求平局,最后才算输 平局很好判断,有向图上,从$S$点

POJ 2240 Arbitrage (spfa判环)

Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 Frenc

HDUOJ--4888--Redraw Beautiful Drawings【isap】网络流+判环

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4888 题意:一个矩阵,限定每行行和.列和,每个格子数字不超过k,问矩阵是否存在,如存在判断有单解还是多解. 思路:之前多校的题目,那时候还不会网络流,现在A掉了,矩阵的建图模型,判断网络流是否可行只要判断最大流是否等于总行和或总列和即可,判环是看的别人的解题报告,方法是使用dfs查找残余网络中是否有还存在容量的弧形成了环,如果有,说明可以通过这个环改变容量网络内部的增广路方式,而源汇的流量是不会变的,就

【BZOJ 3232】圈地游戏 二分+SPFA判环/最小割经典模型

最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那个模型的但是我觉得他会不是一个圈我就否掉了,但是仔细想想的话会发现,如果是这样的话所得到的答案一定小于等于一个圈的答案(浓度),所以我们可定会得到最终答案,所以这样做是可以的,所以说要有宽松得正解的意识(泥沙俱下但沙子不影响我泥).当时我否掉最小割以后就立马去想费用流了,然后想到建图后发现那样建图虽

SDNU 1089.拓扑排序(拓扑判环小顶堆)

Description 给定一个有向图,若图无环,则将其进行拓扑排序并输出,否则输出IMPOSABLE. Input 第一行为两个整数n(1<=n<=1000).m(1<=m<=100000): 之后m行,每行两个整数a.b(0 < a, b <= n)表示一条从a到b的有向边. Output 若存在环,输出IMPOSABLE,否则输出一行用一个空格隔开的拓扑排序的结果,若存在多个结果,输出字典序最小的. Sample Input 5 4 1 2 2 3 3 4 4 5

HDU 5154 Harry and Magical Computer 有向图判环

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5154 题解: 有向图判环. 1.用dfs,正在访问的节点标记为-1,已经访问过的节点标记为1,没有访问过的节点标记为0,如果访问到-1的节点说明说有环. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 t

HDOJ 5154 Harry and Magical Computer floyd判环

floyd判环 Harry and Magical Computer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1005    Accepted Submission(s): 404 Problem Description In reward of being yearly outstanding magic student, H

poj2240 Arbitrage (spfa判环)

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10997   Accepted: 4622 Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currenc

hdu 4324 Triangle LOVE(拓扑判环)

Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3603    Accepted Submission(s): 1416 Problem Description Recently, scientists find that there is love between any of two people. Fo