2368. Reachable Nodes With Restrictions
2368. Reachable Nodes With Restrictions
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)