---
title: "2368. Reachable Nodes With Restrictions"
url: "https://laigary.com/interview/coding/2368-reachable-nodes-with-restrictions"
type: "note"
section: "coding"
date: "2025-03-27"
updated: "2025-10-25"
tags: ["Graph", "Breadth-First Search", "Depth-First Search"]
---

# 2368. Reachable Nodes With Restrictions

[2368\. Reachable Nodes With Restrictions](https://leetcode.com/problems/reachable-nodes-with-restrictions/)

```python
class Solution:
    def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
        
        graph = defaultdict(list)
        
        for edge in edges:
            u, v = edge
            graph[u].append(v)
            graph[v].append(u)
            
        visited = set(restricted)
        
        def dfs(source):
            if source in visited:
                return 0
            visited.add(source)
            count = 1
            nodes = graph[source]
            for node in nodes:
                count += dfs(node)
            return count
        
        return dfs(0)
                
```
