2368. Reachable Nodes With Restrictions

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2368

class Solution:
    def reachableNodes(
        self, n: int, edges: List[List[int]], restricted: List[int]
    ) -> int:
        s = set(restricted)
        g = defaultdict(list)
        for a, b in edges:
            g[a].append(b)
            g[b].append(a)
        q = deque([0])
        vis = [False] * n
        for v in restricted:
            vis[v] = True
        res = 0
        while q:
            i = q.popleft()
            res += 1
            vis[i] = True
            for j in g[i]:
                if not vis[j]:
                    q.append(j)
        return res