[LeetCode] 2316. Count Unreachable Pairs of Nodes in an Undirected Graph

김민우·2023년 3월 25일
0

알고리즘

목록 보기
164/189

- Problem

2316. Count Unreachable Pairs of Nodes in an Undirected Graph

- 내 풀이 (union-find)

class Solution:
    def countPairs(self, n: int, edges: List[List[int]]) -> int:
        def find(x):
            if x != parent[x]:
                parent[x] = find(parent[x])
            return parent[x]
        
        def union(x, y):
            x, y = map(find, (x, y))
            if x < y:
                parent[y] = x
            else:
                parent[x] = y

        parent = [i for i in range(n)]

        for u, v in edges:
            if parent[u] != parent[v]:
                union(u, v)

        counter = list(Counter(find(k) for k in parent).values())
        return reduce(lambda cnt, x: (cnt[0] + x * cnt[1], cnt[1] + x), counter, (0, 0))[0]

- 결과

profile
Pay it forward.

0개의 댓글