Created
August 7, 2022 13:08
-
-
Save Dmitry-Ronzhin/bd3a16e476296f3521dc79f557d62d55 to your computer and use it in GitHub Desktop.
This file contains 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
with open('input_pairs.txt') as f: | |
ids = {} | |
lines = f.readlines() | |
for line in lines: | |
p,q = [int(x) for x in line.split(' ')] | |
if p not in ids: | |
ids[p] = p | |
if q not in ids: | |
ids[q] = q | |
i, j = p, q | |
while i != ids[i]: | |
i = ids[i] | |
while j != ids[j]: | |
j = ids[j] | |
if i == j: | |
continue | |
ids[i] = j | |
print(p,q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment