Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import sys
  2. from Queue import *
  3.  
  4.  
  5. N, M, K = map(int, raw_input().split())
  6. graph = {}
  7. for i in range(M):
  8.     p, q = map(int, raw_input().split())
  9.     if p not in graph:
  10.         graph[p] = [q]
  11.         if q not in graph:
  12.             graph[q] = [p]
  13.         else:
  14.             graph[q].append(p)
  15.     elif q not in graph:
  16.         graph[q] = [p]
  17.         graph[p].append(q)
  18.     else:
  19.         graph[p].append(q)
  20.         graph[q].append(p)
  21.  
  22. #print(graph)
  23. ks = map(int, raw_input().split())
  24. ks.sort
  25.  
  26. smallestDist = sys.maxint
  27. smallestX = sys.maxint
  28. smallestY = sys.maxint
  29.  
  30. def bfs(graph, s):
  31.     dist = {}
  32.     global smallestDist
  33.     global smallestX
  34.     global smallestY
  35.     global ks
  36.     q = Queue()
  37.     # for k in ks:
  38.     #   q.put(k)
  39.     dist[s] = 0
  40.     q.put(s)
  41.     while(not q.empty()):
  42.         v = q.get()
  43.         for w in graph[v]:
  44.             isWK = (w in ks)
  45.             if w not in dist:
  46.                 q.put(w)
  47.                 curDist = dist[v]+1
  48.                 if curDist < smallestDist and isWK:
  49.                     smallestDist = curDist
  50.                     smallestX = s
  51.                     smallestY = w
  52.                 dist[w] = curDist
  53.  
  54.  
  55. for k in ks:
  56.     bfs(graph, k)
  57.  
  58. smallerone = min(smallestX,smallestY)
  59. biggerone = max(smallestX, smallestY)
  60. #print(" ")
  61. print("{} {}".format(smallerone, biggerone))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement