882. Reachable Nodes In Subdivided Graph LeetCode Solution
In this guide, you will get 882. Reachable Nodes In Subdivided Graph LeetCode Solution with the best time and space complexity. The solution to Reachable Nodes In Subdivided Graph problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Reachable Nodes In Subdivided Graph solution in C++
- Reachable Nodes In Subdivided Graph solution in Java
- Reachable Nodes In Subdivided Graph solution in Python
- Additional Resources

Problem Statement of Reachable Nodes In Subdivided Graph
You are given an undirected graph (the “original graph”) with n nodes labeled from 0 to n – 1. You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.
The graph is given as a 2D array of edges where edges[i] = [ui, vi, cnti] indicates that there is an edge between nodes ui and vi in the original graph, and cnti is the total number of new nodes that you will subdivide the edge into. Note that cnti == 0 means you will not subdivide the edge.
To subdivide the edge [ui, vi], replace it with (cnti + 1) new edges and cnti new nodes. The new nodes are x1, x2, …, xcnti, and the new edges are [ui, x1], [x1, x2], [x2, x3], …, [xcnti-1, xcnti], [xcnti, vi].
In this new graph, you want to know how many nodes are reachable from the node 0, where a node is reachable if the distance is maxMoves or less.
Given the original graph and maxMoves, return the number of nodes that are reachable from node 0 in the new graph.
Example 1:
Input: edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3
Output: 13
Explanation: The edge subdivisions are shown in the image above.
The nodes that are reachable are highlighted in yellow.
Example 2:
Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4
Output: 23
Example 3:
Input: edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5
Output: 1
Explanation: Node 0 is disconnected from the rest of the graph, so only node 0 is reachable.
Constraints:
0 <= edges.length <= min(n * (n – 1) / 2, 104)
edges[i].length == 3
0 <= ui < vi < n
There are no multiple edges in the graph.
0 <= cnti <= 104
0 <= maxMoves <= 109
1 <= n <= 3000
Complexity Analysis
- Time Complexity: O((|V| + |E|)\log |V|)
- Space Complexity: O(|E| + |V|)
882. Reachable Nodes In Subdivided Graph LeetCode Solution in C++
class Solution {
public:
int reachableNodes(vector<vector<int>>& edges, int maxMoves, int n) {
vector<vector<pair<int, int>>> graph(n);
vector<int> dist(graph.size(), maxMoves + 1);
for (const vector<int>& edge : edges) {
const int u = edge[0];
const int v = edge[1];
const int cnt = edge[2];
graph[u].emplace_back(v, cnt);
graph[v].emplace_back(u, cnt);
}
const int reachableNodes = dijkstra(graph, 0, maxMoves, dist);
int reachableSubnodes = 0;
for (const vector<int>& edge : edges) {
const int u = edge[0];
const int v = edge[1];
const int cnt = edge[2];
// the number of reachable nodes of `edge` from `u`
const int a = dist[u] > maxMoves ? 0 : min(maxMoves - dist[u], cnt);
// the number of reachable nodes of `edge` from `v`
const int b = dist[v] > maxMoves ? 0 : min(maxMoves - dist[v], cnt);
reachableSubnodes += min(a + b, cnt);
}
return reachableNodes + reachableSubnodes;
}
private:
int dijkstra(const vector<vector<pair<int, int>>>& graph, int src,
int maxMoves, vector<int>& dist) {
dist[src] = 0;
using P = pair<int, int>; // (d, u)
priority_queue<P, vector<P>, greater<>> minHeap;
minHeap.emplace(dist[src], src);
while (!minHeap.empty()) {
const auto [d, u] = minHeap.top();
minHeap.pop();
// Already took `maxMoves` to reach `u`, so can't explore anymore.
if (d >= maxMoves)
break;
if (d > dist[u])
continue;
for (const auto& [v, w] : graph[u])
if (d + w + 1 < dist[v]) {
dist[v] = d + w + 1;
minHeap.emplace(dist[v], v);
}
}
return ranges::count_if(dist, [&](int d) { return d <= maxMoves; });
}
};
/* code provided by PROGIEZ */
882. Reachable Nodes In Subdivided Graph LeetCode Solution in Java
class Solution {
public int reachableNodes(int[][] edges, int maxMoves, int n) {
List<Pair<Integer, Integer>>[] graph = new List[n];
int[] dist = new int[n];
Arrays.fill(dist, maxMoves + 1);
for (int i = 0; i < n; ++i)
graph[i] = new ArrayList<>();
for (int[] edge : edges) {
final int u = edge[0];
final int v = edge[1];
final int cnt = edge[2];
graph[u].add(new Pair<>(v, cnt));
graph[v].add(new Pair<>(u, cnt));
}
final int reachableNodes = dijkstra(graph, 0, maxMoves, dist);
int reachableSubnodes = 0;
for (int[] edge : edges) {
final int u = edge[0];
final int v = edge[1];
final int cnt = edge[2];
// the number of reachable nodes of `edge` from `u`
final int a = dist[u] > maxMoves ? 0 : Math.min(maxMoves - dist[u], cnt);
// the number of reachable nodes of `edge` from `v`
final int b = dist[v] > maxMoves ? 0 : Math.min(maxMoves - dist[v], cnt);
reachableSubnodes += Math.min(a + b, cnt);
}
return reachableNodes + reachableSubnodes;
}
private int dijkstra(List<Pair<Integer, Integer>>[] graph, int src, int maxMoves, int[] dist) {
dist[src] = 0;
Queue<Pair<Integer, Integer>> minHeap =
new PriorityQueue<>(Comparator.comparing(Pair::getKey)) {
{ offer(new Pair<>(dist[src], src)); } // (d, u)
};
while (!minHeap.isEmpty()) {
final int d = minHeap.peek().getKey();
final int u = minHeap.poll().getValue();
// Already took `maxMoves` to reach `u`, so can't explore anymore.
if (d >= maxMoves)
break;
if (d > dist[u])
continue;
for (Pair<Integer, Integer> pair : graph[u]) {
final int v = pair.getKey();
final int w = pair.getValue();
if (d + w + 1 < dist[v]) {
dist[v] = d + w + 1;
minHeap.offer(new Pair<>(dist[v], v));
}
}
}
return (int) Arrays.stream(dist).filter(d -> d <= maxMoves).count();
}
}
// code provided by PROGIEZ
882. Reachable Nodes In Subdivided Graph LeetCode Solution in Python
class Solution:
def reachableNodes(
self,
edges: list[list[int]],
maxMoves: int,
n: int,
) -> int:
graph = [[] for _ in range(n)]
dist = [maxMoves + 1] * n
for u, v, cnt in edges:
graph[u].append((v, cnt))
graph[v].append((u, cnt))
reachableNodes = self._dijkstra(graph, 0, maxMoves, dist)
reachableSubnodes = 0
for u, v, cnt in edges:
# the number of reachable nodes of (u, v) from `u`
a = 0 if dist[u] > maxMoves else min(maxMoves - dist[u], cnt)
# the number of reachable nodes of (u, v) from `v`
b = 0 if dist[v] > maxMoves else min(maxMoves - dist[v], cnt)
reachableSubnodes += min(a + b, cnt)
return reachableNodes + reachableSubnodes
def _dijkstra(
self,
graph: list[list[tuple[int, int]]],
src: int,
maxMoves: int,
dist: list[int],
) -> int:
dist[src] = 0
minHeap = [(dist[src], src)] # (d, u)
while minHeap:
d, u = heapq.heappop(minHeap)
# Already took `maxMoves` to reach `u`, so can't explore anymore.
if dist[u] >= maxMoves:
break
if d > dist[u]:
continue
for v, w in graph[u]:
newDist = d + w + 1
if newDist < dist[v]:
dist[v] = newDist
heapq.heappush(minHeap, (newDist, v))
return sum(d <= maxMoves for d in dist)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.