1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| from collections import deque
import sys
input = sys.stdin.readline
N, M, K, X = map(int, input().split())
nodes = [[] for _ in range(N+1)]
visited = [False] * (N+1)
dists = [0] * (N+1)
for _ in range(M):
A, B = map(int, input().split())
nodes[A].append(B)
queue = deque()
queue.append(X)
visited[X] = True
while queue:
city = queue.popleft()
for next in nodes[city]:
if not visited[next] and dists[city] < K:
queue.append(next)
visited[next] = True
dists[next] = dists[city]+1
targets = [i for i,d in enumerate(dists) if d==K]
if targets:
for target in targets:
print(target)
else:
print(-1)
|