


You are given an undirected graph with ‘n’ nodes labeled from ‘0’ to ‘n - 1’. The graph is given in as an array of ‘edges’, where ‘edges[i] = [u, v, sz]’ means there is an edge between node ‘u’ and ‘v’ in the given graph. Create a new graph by replacing the edge between ‘u’ and ‘v’ with a chain of size ‘sz’.
To create a chain of size ‘sz’ between nodes ‘(u, v)’, create ‘sz’ new nodes and ‘sz + 1’ new edges. The new nodes are ‘X1, X2, … , Xsz’ and the new edges are ‘(u, X1), (X1, X2), (X2, X3), … , (X(sz-1), Xsz), (Xsz, v)’.
A node is reachable from node ‘0’ if it’s at a distance of less than or equal to ‘m’ edges. Your task is to find the number of nodes reachable from node ‘0’ in the new graph.
Example:‘n = 3’, ‘m = 4’
‘edges = [ [0, 2, 8], [0, 1, 1], [1, 2, 0] ]’

The reachable nodes are highlighted in red color. So, the answer is ‘9’. Node ‘0’, itself included in the answer.
Note:
1. For an edge ‘[u, v, sz]’, if ‘sz = 0’, then there is no chain, and ‘(u, v)’ is an edge in the new graph.
2. The graph doesn’t contain parallel edges or self-loops.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
The first line of each test case contains three integers ‘n’, ‘m’, and ‘numOfPairs’, denoting the number of nodes in the graph, the maximum allowed distance from node ‘0’, and the size of the ‘edges’ array. Then, ‘numOfPairs’ lines follow.
Each line contains three integers representing an element in ‘edges’.
Output format:
For every test case, print a single line containing a single integer denoting the number of nodes reachable from node ‘0’ in the new graph.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= n <= 500
0 <= m <= 10 ^ 9
0 <= numOfPairs <= min(n * (n - 1) / 2, 10^3)
0 <= edges[i].u < edges[i].v < n
0 <= edges[i].sz <= 10^5
Each element of ‘edges’ contains exactly three integers.
Where ‘T’ is the number of test cases, ‘n’ is the number of nodes, ‘m’ is the maximum allowed distance from node ‘0’, and ‘numOfPairs’ is the size of the array ‘edges’.
Time limit: 1 second
2
4 3 5
0 1 4
1 3 0
0 3 1
0 2 1
2 3 4
3 7 2
0 1 5
1 2 3
11
8
Test Case 1:
‘n = 4’, ‘m = 3’
‘edges = [ [0, 1, 4], [1, 3, 0], [0, 3, 1], [0, 2, 1], [2, 3, 4] ]’

The reachable nodes are highlighted in red color. So, the answer is ‘11’.
Test Case 2:
‘n = 3’, ‘m = 7’
‘edges = [ [0, 1, 5], [1, 2, 3] ]’

The reachable nodes are highlighted in red color. So, the answer is ‘8’.
2
5 16 5
1 2 4
2 3 5
3 4 10
1 4 6
2 4 1
4 8 4
0 1 3
0 2 6
1 2 4
1 3 1
1
18
Try to create the new graph from ‘edges’.
Create the new graph from the original graph by adding the chain nodes. The problem becomes finding the number of nodes whose shortest path from node ‘0’ is less than equal to ‘m’. As the new graph is unweighted, we can use Breadth-first search (BFS) traversal to find the shortest path. The shortest path length will be equivalent to the node’s level in the BFS traversal tree. Perform BFS up to level ‘m’, as we only want the nodes with path length less than equal to ‘m’.
Proof of correctness: As we move level-wise in BFS, if a child node is not visited, it’s not reachable from any previous levels. Its shortest path will be equal to (parent node’s level + 1). Even if it’s reachable from any other node from the next levels, the corresponding path length will always be greater.
Following is an implementation of BFS to solve this problem:
O(N + SZ * NumOfPairs), where ‘N’ is the number of nodes in the given graph, ‘SZ’ is the number of chain nodes in a single edge, and 'NumOfPairs’ is the number of edges.
The time complexity for BFS traversal when an adjacency list is used is ‘O(V + E)’, where ‘V’ is the number of vertices and ‘E’ is the number of edges in the graph. In the worst-case scenario, each edge has the maximum number of chain nodes ‘sz’ (i.e., 10 ^ 4 in this problem), so for the new graph, we have ‘V = N + SZ * NumOfPairs’ and ‘E = NumOfPairs + NumOfPairs * SZ’. Hence, the time complexity of ‘O(N + SZ * NumOfPairs)’.
O(N + SZ * NumOfPairs), where ‘N’ is the number of nodes in the given graph, ‘SZ’ is the number of chain nodes in a single edge, and ‘NumOfPairs’ is the number of edges.
Space required by the adjacency list used to store the new graph is ‘O(E)’, the ‘seen’ array is ‘O(E)’, and the ‘bfsQ’ is ‘O(V). Here, ‘V’ is the number of vertices, and ‘E’ is the number of edges. In the worst-case scenario, for the new graph we have ‘V = N + SZ * NumOfPairs’ and ‘E = NumOfPairs + NumOfPairs * SZ’. Hence, the space complexity of ‘O(N + SZ * NumOfPairs)’.