-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolution.py
More file actions
42 lines (35 loc) · 1.19 KB
/
solution.py
File metadata and controls
42 lines (35 loc) · 1.19 KB
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
33
34
35
36
37
38
39
40
41
42
def find_reachable_vertices(n, edges):
from collections import defaultdict, deque
# Построение обратного графа
reverse_graph = defaultdict(list)
for u, v in edges:
reverse_graph[v].append(u)
# Поиск в ширину (BFS) из первой вершины в обратном графе
reachable = set()
queue = deque([1])
while queue:
vertex = queue.popleft()
if vertex not in reachable:
reachable.add(vertex)
for neighbor in reverse_graph[vertex]:
if neighbor not in reachable:
queue.append(neighbor)
# Возвращаем отсортированный список достижимых вершин
return sorted(reachable)
def main():
import sys
input = sys.stdin.read
data = input().split()
n = int(data[0])
m = int(data[1])
edges = []
index = 2
for _ in range(m):
u = int(data[index])
v = int(data[index + 1])
edges.append((u, v))
index += 2
reachable_vertices = find_reachable_vertices(n, edges)
print(" ".join(map(str, reachable_vertices)))
if __name__ == "__main__":
main()