882 Reachable Nodes In Subdivided Graph
1. Question
Starting with an undirected graph (the "original graph") with nodes from0
toN-1
, subdivisions are made to some of the edges.
The graph is given as follows:edges[k]
is a list of integer pairs(i, j, n)
such that(i, j)
is an edge of the original graph,
andn
is the total number ofnewnodes on that edge.
Then, the edge(i, j)
is deleted from the original graph, n
new nodes(x_1, x_2, ..., x_n)
are added to the original graph,
andn+1
new edges(i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j)
are added to the original graph.
Now, you start at node0
from the original graph, and in each move, you travel along one edge.
Return how many nodes you can reach in at mostM
moves.
Example 1:
Example 2:
Note:
0 <= edges.length <= 10000
0 <= edges[i][0] < edges[i][1] < N
There does not exist any
i != j
for whichedges[i][0] == edges[j][0]
andedges[i][1] == edges[j][1]
.The original graph has no parallel edges.
0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
A reachable node is a node that can be travelled to using at most M moves starting from node 0.
2. Implementation
思路: 首先要明白一个定义:A reachable node is a node that can be reached within M moves starting from the original one.
(1)
3. Time & Space Complexity
时间复杂度O(), 空间复杂度O()
Last updated
Was this helpful?