Guest User

Untitled

a guest
Sep 21st, 2017
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. def BFS():
  2.     global edges, mark, index, queue, parents
  3.     if index >= len(queue):
  4.         return False
  5.     node = queue[index]
  6.     index += 1
  7.     if mark[node]:
  8.         BFS(finish)
  9.         return False
  10.     mark[node] = True
  11.     for child in range(len(edges[node])):
  12.         if (edges[node][child]) and parents[child] == -1:
  13.             parents[child] = node
  14.             queue.append(child)
  15.     BFS()
  16.  
  17. n = int(input())
  18. m = int(input())
  19. edges = [0]*n
  20. for i in range(n):
  21.     edges[i] = [0] * n
  22. for i in range(m):
  23.     a, b = map(int, input().split())
  24.     edges[a][b] += 1
  25. mark = [False] * n
  26. parents = [-1]*n
  27. parents[0] = -2
  28. start, finish = map(int, input().split())
  29. queue = [start]
  30. index = 0
  31. BFS()
  32. print(mark)
  33. print(parents)
Advertisement
Add Comment
Please, Sign In to add comment