Pagini recente » Cod sursa (job #2121831) | Cod sursa (job #880893) | Cod sursa (job #463539) | Cod sursa (job #583913) | Cod sursa (job #2489342)
# file = open("E:\PyCharmProjects\data.in")
# fileout=open("E:\PyCharmProjects\\bfs.out","w")
file = open("bfs.in")
fileout = open("bfs.out", "w")
def BFS(nod):
S = [0] * 10
cost = [-1] * 10
L = 1
i = 1
S[L] = nod
cost[nod] = 0
while i <= L:
if adiacenta.get(S[i]):
for j in adiacenta.get(S[i]):
if cost[j] == -1:
L += 1
S[L] = j
cost[S[L]] = cost[S[i]] + 1
i += 1
fileout.write(str(cost[1:(N + 1)]))
x = file.readline().split()
N = int(x[0])
M = int(x[1])
S = int(x[2])
adiacenta = {int(i): set() for i in range(1, N + 1)}
for i in range(M):
x = file.readline().split()
adiacenta[int(x[0])].add(int(x[1]))
BFS(S)
file.close()