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

The meaning of the question: give you a directed graph, starting from a given starting point, if you can find a point with an out-degree of 0 and an even number of points between the starting point and the current point, output "Win" and These points passed, if there is no such point but there is a loop, output "Draw", otherwise output "lose".

Solution: There is no doubt about DFS, but there are a lot of things to be aware of.
(1) Pay attention to the mark when dfs. When you go to a point that has been passed, if you have not traveled before, go down. If the number of steps you have walked has the same parity as the number of previous steps Directly drop from continue, otherwise continue walking, which greatly reduces the time complexity. In some cases, if this path is not feasible, there is a ring in the middle to use this ring to stagger the parity gap (wa28).
(2) Two marker arrays are required, one marker to see if there is a ring, to prepare for the Dram, and one marker to see if it has passed.
(3) If it exceeds 1e6 points, it will not work.

Code:

#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;//Record whether to take the heavy road (single and even numbers to mark)
		use2[en]=1;//Whether the record is a loop,
		dfs(en,num+1);
		use2 [en] = 0;		
	}
}
intmain()
{
	  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);//Save edge

			}
		}
		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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325614965&siteId=291194637
dfs