Union-Find
Basic implementation
class UnionFind: def __init__(self): self.p = {} def find(self, u): if self.p[u] != u: self.p[u] = self.find(self.p[u]) return self.p[u] def union(self, u, v): self.p.setdefault(u, u) self.p.setdefault(v, v) self.p[self.find(u)] = self.find(v)
Cited by 7
- Leetcode: 2158. Amount of New Area Painted Each Day
- Leetcode: 924. Minimize Malware Spread
- Leetcode: 1632. Rank Transform of a Matrix
- Leetcode: 839. Similar String Groups
- Leetcode: 778. Swim in Rising Water
- Leetcode: 2092. Find All People With Secret
- Leetcode: 1697. Checking Existence of Edge Length Limited Paths