-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path936B.py
75 lines (58 loc) · 1.32 KB
/
936B.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# http://codeforces.com/problemset/problem/936/B
# TODO - TL in Test 9
G = {}
has_cycle = False
last_v = None
parents = {}
colors = {}
final_vs = []
def scanner(s):
return map(int, s.split())
def dfs2(u):
global G, last_v, has_cycle, final_vs, colors, parents
stack = [u]
while len(stack):
v = stack.pop()
if v in final_vs:
last_v = v
return
for w in G[v]:
if colors[w] == 0:
stack.append(w)
parents[w] = v
elif colors[w] == 1:
has_cycle = True
colors[v] = 1
n, m = scanner(input())
for i in range(n):
s0 = (i, 0)
s1 = (i, 1)
G[s0] = []
G[s1] = []
row = list(scanner(input()))
ci = row[0]
if ci == 0:
final_vs.append((i, 0))
continue
for j in range(1, ci + 1):
v = row[j] - 1
G[s0].append((v, 1))
G[s1].append((v, 0))
source = (int(input())-1, 1)
colors = {key: 0 for key in G.keys()}
dfs2(source)
if last_v:
print('Win')
current = last_v
path = []
while True:
path.append(current[0] + 1)
if current in parents:
current = parents[current]
else:
break
print(' '.join(map(str, reversed(path))))
elif has_cycle:
print('Draw')
else:
print('Lose')