- Give each element a component representative.
- Merge representatives when a connection is accepted.
- Answer connectivity or component queries from the compressed representatives.
Code notes
- 53 lines of Python from the credited upstream file 2685.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Account for every find and union operation; with path compression and ranked merging, the amortized cost is nearly constant per operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class UnionFind:2 def __init__(self, n: int):3 self.id = list(range(n))4 self.rank = [0] * n5 self.nodeCount = [1] * n6 self.edgeCount = [0] * n7 8 def unionByRank(self, u: int, v: int) -> None:9 i = self.find(u)10 j = self.find(v)11 self.edgeCount[i] += 112 if i == j:13 return14 if self.rank[i] < self.rank[j]:15 self.id[i] = j16 self.edgeCount[j] += self.edgeCount[i]17 self.nodeCount[j] += self.nodeCount[i]18 elif self.rank[i] > self.rank[j]:19 self.id[j] = i20 self.edgeCount[i] += self.edgeCount[j]21 self.nodeCount[i] += self.nodeCount[j]22 else:23 self.id[i] = j24 self.edgeCount[j] += self.edgeCount[i]25 self.nodeCount[j] += self.nodeCount[i]26 self.rank[j] += 127 28 def find(self, u: int) -> int:29 if self.id[u] != u:30 self.id[u] = self.find(self.id[u])31 return self.id[u]32 33 def isComplete(self, u):34 return self.nodeCount[u] * (self.nodeCount[u] - 1) 2 == self.edgeCount[u]35 36 37class Solution:38 def countCompleteComponents(self, n: int, edges: list[list[int]]) -> int:39 ans = 040 uf = UnionFind(n)41 parents = set()42 43 for u, v in edges:44 uf.unionByRank(u, v)45 46 for i in range(n):47 parent = uf.find(i)48 if parent not in parents and uf.isComplete(parent):49 ans += 150 parents.add(parent)51 52 return ans53