-
Notifications
You must be signed in to change notification settings - Fork 0
/
t31.py
39 lines (31 loc) · 904 Bytes
/
t31.py
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
import sys
n_vert, n_edges = map(int, input().split())
graph = {}
for line in sys.stdin.readlines():
line = line.strip()
if line: # FIX: empty lines in tests
a,b = line.split()
a,b = int(a), int(b)
graph.setdefault(a,set()).add(b)
graph.setdefault(b,set()).add(a)
def solve(graph, vertex=1):
ret = [vertex] # initial vertex always included
if not graph:
return ret
nvert = max(graph.keys())
visited = [False] * (nvert + 1) # count from 1
stack = [vertex]
while stack:
s = stack.pop()
if visited[s]:
continue
visited[s] = True
if s in graph:
for xx in graph[s]:
if not visited[xx]:
stack.append(xx)
ret = [i for i,v in enumerate(visited) if v]
return ret
ans = list(sorted(solve(graph, 1)))
print(len(ans))
print(*ans)