Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created September 24, 2023 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughdbrown/da0531c27a23df79311582a74dc0b3f8 to your computer and use it in GitHub Desktop.
Save hughdbrown/da0531c27a23df79311582a74dc0b3f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
from collections import defaultdict
line = sys.stdin.readline()
n = int(line.split(' ')[0])
d = defaultdict(list)
for line in sys.stdin.readlines():
src, dst = [int(x) for x in line.split(' ')]
d[src].append(dst)
d[dst].append(src)
unvisited = set(range(1, n + 1))
q = [1]
while q:
r = []
for a in q:
unvisited.discard(a)
for neighbor in d[a]:
if neighbor in unvisited:
r.append(neighbor)
q = r
if not unvisited:
print("Connected")
else:
for x in sorted(unvisited):
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment