fork download
  1. # your code goes here
  2. import sys
  3. from collections import deque
  4.  
  5. input = sys.stdin.readline
  6.  
  7. n, m, k, x = map(int, input().split())
  8. lst = [[] for _ in range(n + 1)]
  9. for _ in range(m):
  10. a, b = map(int, input().split())
  11. lst[a].append(b)
  12.  
  13. q = deque()
  14. q.append([x, 0])
  15. visited = [0] * (n + 1)
  16. visited[x] = 1
  17. flag = False
  18.  
  19. while q:
  20. node, dist = q.popleft()
  21.  
  22. if dist == k:
  23. print(node)
  24. flag = True
  25.  
  26. for nxt_node in lst[node]:
  27. if not visited[nxt_node]:
  28. visited[nxt_node] = 1
  29. q.append([nxt_node, dist + 1])
  30.  
  31. if not flag:
  32. print(-1)
Success #stdin #stdout 0.03s 9756KB
stdin
5 4 2 1
1 2
1 3
2 5
3 4
stdout
5
4