1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const numWays = (n, relation, k) => { const t = n - 1, g = new Array(n).fill(0).map(() => new Array()); let ans = 0; for (const [u, v] of relation) g[u].push(v); const dfs = (u, steps) => { if (steps == k) { if (u == t) ++ans; return; } for (const v of g[u]) dfs(v, steps + 1); } dfs(0, 0); return ans; };
|