Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from Queue import *
- N, M, K = map(int, raw_input().split())
- graph = {}
- for i in range(M):
- p, q = map(int, raw_input().split())
- if p not in graph:
- graph[p] = [q]
- if q not in graph:
- graph[q] = [p]
- else:
- graph[q].append(p)
- elif q not in graph:
- graph[q] = [p]
- graph[p].append(q)
- else:
- graph[p].append(q)
- graph[q].append(p)
- #print(graph)
- ks = map(int, raw_input().split())
- ks.sort
- smallestDist = sys.maxint
- smallestX = sys.maxint
- smallestY = sys.maxint
- def bfs(graph, s):
- dist = {}
- global smallestDist
- global smallestX
- global smallestY
- global ks
- q = Queue()
- # for k in ks:
- # q.put(k)
- dist[s] = 0
- q.put(s)
- while(not q.empty()):
- v = q.get()
- for w in graph[v]:
- isWK = (w in ks)
- if w not in dist:
- q.put(w)
- curDist = dist[v]+1
- if curDist < smallestDist and isWK:
- smallestDist = curDist
- smallestX = s
- smallestY = w
- dist[w] = curDist
- for k in ks:
- bfs(graph, k)
- smallerone = min(smallestX,smallestY)
- biggerone = max(smallestX, smallestY)
- #print(" ")
- print("{} {}".format(smallerone, biggerone))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement