Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def BFS():
- global edges, mark, index, queue, parents
- if index >= len(queue):
- return False
- node = queue[index]
- index += 1
- if mark[node]:
- BFS(finish)
- return False
- mark[node] = True
- for child in range(len(edges[node])):
- if (edges[node][child]) and parents[child] == -1:
- parents[child] = node
- queue.append(child)
- BFS()
- n = int(input())
- m = int(input())
- edges = [0]*n
- for i in range(n):
- edges[i] = [0] * n
- for i in range(m):
- a, b = map(int, input().split())
- edges[a][b] += 1
- mark = [False] * n
- parents = [-1]*n
- parents[0] = -2
- start, finish = map(int, input().split())
- queue = [start]
- index = 0
- BFS()
- print(mark)
- print(parents)
Advertisement
Add Comment
Please, Sign In to add comment