Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def __init__(self, n):
- self.data = [i for i in range(1, n)]
- def connected(self, p, q):
- return self.data[q] == self.data[p]
- def union(self, p, q):
- pid = self.data[p]
- qid = self.data[q]
- for i in range(1, len(self.data)):
- if self.data[i] == pid:
- self.data[i] = qid
- def main():
- uf = UF(10)
- print(uf.connected(1, 3))
- uf.union(1, 3)
- print(uf.connected(1, 3))
- uf.union(3, 8)
- print(uf.connected(1, 8))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement