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
|