lc2368.受限条件下可到达节点的数目

题目链接:

题解

简单 dfs

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#
# @lc app=leetcode.cn id=2368 lang=python3
#
# [2368] 受限条件下可到达节点的数目
#

# @lc code=start
class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
g = [[] for i in range(n)]
for e in edges:
g[e[0]].append(e[1])
g[e[1]].append(e[0])
ok = set(restricted)

ans = 0

def dfs(rt, fa):
if rt in ok:
return 0
ret = 1
for nx in g[rt]:
if nx == fa:
continue
ret += dfs(nx, rt)
return ret
return dfs(0, -1)
# @lc code=end


lc2368.受限条件下可到达节点的数目

https://blog.xiang578.com/problem/lc2368.html

作者

Ryen Xiang

发布于

2024-03-02

更新于

2024-04-20

许可协议


网络回响

评论