Reachable Nodes In Subdivided Graph

HardLinked ListGraph

Description

Edges are subdivided into nodes. Starting from node 0 with maxMoves, find how many nodes (including subdivided nodes) are reachable.

Examples

Input:edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
Output:13
Explanation:

With 6 moves from node 0: reach all 3 original nodes plus 10 subdivided edge nodes.

Input:edges = [[1]], maxMoves = 6, n = 3
Output:1
Explanation:

With no valid edge from node 0, only node 0 is reachable, so the answer is 1.

Input:edges = [[0,1,4],[1,2,3],[2,3,1]], maxMoves = 8, n = 4
Output:9
Explanation:

Starting from node 0 with 8 moves: node 1 is reached in 4 moves, node 2 in 7 moves (4+3), and node 3 in 8 moves (7+1). This covers all 4 original nodes plus 7 subdivided nodes along the edges, totaling 11 reachable nodes.

Constraints

  • 0 ≤ edges.length ≤ min(n*(n-1)/2, 10⁴)

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!