Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n, m = map(int, input().split())
- g = [[] for i in range(n)]
- for i in range(m):
- x, y = map(int, input().split())
- x -= 1
- y -= 1
- g[x].append(y)
- g[y].append(x)
- used = [False] * n
- #дфс на стеке
- s = [0]
- while len(s) != 0:
- h = s.pop()
- used[h] = True
- for e in g[h]:
- if not used[e]:
- s.append(e)
Advertisement
Add Comment
Please, Sign In to add comment