Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from queue import Queue
- 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
- q = Queue()
- q.put(0)
- used[0] = True
- while not q.empty():
- cur = q.get()
- for nxt in g[cur]:
- if not used[nxt]:
- used[nxt] = True
- q.put(nxt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement