Codeforces dfs

  
D. Sleepy Game
time limit per test
2 seconds
memory limit per test
256 megabytes
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
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

题意:给你你一个有向图,从一个给定的起点开始,如果可以找到一个出度为0且从起点到当前点的之间点的个数为偶数的点时输出“Win”和走过的这些点,如果没有这样的点但是有环则输出“Draw”,否则输出“lose”。

题解:毫无疑问DFS,但是有很多需要值得注意的地方。
(1)dfs的时候要注意标记,当走到一个已经走过的点的时候如果是以前没有走过的话则走下去,如果是走过的步数和以前的步数有相同的奇偶性的话直接从continue掉,否则继续走,这样大大减少了时间复杂度,有些情况就是走这条路不行的话,在其中间有个环利用这个环可以错开奇偶差距(wa28)。
(2)需要两个标记数组,一个标记看是否出现环,为Dram做准备,一个标记是否走过。
(3)如果超过1e6个点的话也是不行的。

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include<map>
#include<vector>
#define qq printf("QAQ\n");
#define ll long long
#define p(num) printf("%d\n",num);
using namespace std;
const int maxn=1e5+5;
const int mod=1e9+7;
const int inf=1e9;
vector<int>v[maxn];
int use1[maxn][2],use2[maxn];
int a[1000005],f;
void dfs(int st,int num)
{
	if(num>=1000000)return ;
	a[num]=st;
	use1[st][num%2]=1;	
	if(!v[st].size()&&num%2==0){
		printf("Win\n");
		for(int i=1;i<=num;i++)
		if(i!=1)printf(" %d",a[i]);
		else printf("%d",a[i]);
		f=1;
		printf("\n");
		exit(0);///找到路了直接退出 
	//	return ;
	}
	for(int i=0;i<v[st].size();i++)
	{
		//if(f==1)return ;
		int en=v[st][i];		
		if(use2[en])f=2;
		if(use1[en][(num+1)%2])continue;//记录是否走重的路(单双数来标记) 
		use2[en]=1;//记录是否成环, 
		dfs(en,num+1);
		use2[en]=0;		
	}
}
int main()
{
	  int n,m,st,en,x;
	  scanf("%d%d",&n,&m);
  		for(int i=1;i<=n;i++)
  		{
		  scanf("%d",&x);
		  for(int j=1;j<=x;j++){
		  	scanf("%d",&en);
		  	v[i].push_back(en);//存边 

			}
		}
		scanf("%d",&st);
		f=0;
		use2[st]=1;
		dfs(st,1);
		if(f==0)printf("Lose\n");
		if(f==2)printf("Draw\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值