Skip to content

Instantly share code, notes, and snippets.

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