Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. def bfs(start, graph):
  2.     visited = set()
  3.     queue = []
  4.     queue.append(start)
  5.     dist = 6
  6.     results = {}
  7.     while queue:
  8.         node = queue.pop(0)
  9.         if node not in visited:
  10.             visited.add(node)
  11.             queue.extend(graph[node] - visited)
  12.             for i in graph[node]:
  13.                 if i not in results and i != start:
  14.                     results[i] = dist
  15.             dist += 6
  16.     return results
  17.  
  18. t = int(input())
  19. for z in range(t):
  20.     n, m = [int(x) for x in input().split()]
  21.     graph = {}
  22.     for i in range(n):
  23.         graph[i + 1] = set()
  24.  
  25.     for i in range(0, m):
  26.         a, b = [int(x) for x in input().split()]
  27.         graph[a].add(b)
  28.         graph[b].add(a)
  29.  
  30.     s = int(input())
  31.     x = bfs(s, graph)
  32.    
  33.     for i in range(1, n + 1):
  34.         if i not in x and i != s:
  35.             x[i] = -1
  36.          
  37.     for i in range(1, n + 1):
  38.         if i != s:
  39.             print(x[i], end=' ')
  40.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement