Created
September 24, 2023 13:23
-
-
Save hughdbrown/da0531c27a23df79311582a74dc0b3f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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