-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oving8
58 lines (44 loc) · 1.09 KB
/
Oving8
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
import sys
from collections import deque
class Node:
def __init__(self):
self.child = []
self.ratatosk = False
self.depth = 0
def dfs(root):
stack = deque([root])
while stack:
current = stack.pop()
if current.ratatosk:
return current.depth
for n in current.child:
if n != None:
n.depth = current.depth + 1
stack.append(n)
def bfs(root):
queue = deque([root])
while queue:
current = queue.pop()
if current.ratatosk:
return current.depth
for n in current.child:
if n != None:
n.depth = current.depth + 1
queue.appendleft(n)
def main():
sys.stdin.readline(),sys.stdin.readline()
level=0
d=dict()
root=sys.stdin.readline().strip()
ratatosk=sys.stdin.readline().strip()
for line in sys.stdin:
children=line.split()
parent=children.pop(0)
for child in children:
d[child]=parent
while ratatosk in d:
ratatosk=d[ratatosk]
level+=1
if(ratatosk==root):
return level
print(main())