Skip to content

Instantly share code, notes, and snippets.

@c-yan
Created August 24, 2019 02:59
Show Gist options
  • Save c-yan/e0eb355e01b1c732c94820bd8d3d3026 to your computer and use it in GitHub Desktop.
Save c-yan/e0eb355e01b1c732c94820bd8d3d3026 to your computer and use it in GitHub Desktop.
parent = [-1] * n
def find(parent, i):
if parent[i] < 0:
return i
parent[i] = find(parent, parent[i])
return parent[i]
def unite(parent, i, j):
i = find(parent, i)
j = find(parent, j)
if i == j:
return
parent[j] += parent[i]
parent[i] = j
def size(parent, i):
return -parent[find(parent, i)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment