Loading problem...
You are working with a directed acyclic graph (DAG) representing a dependency system containing numNodes nodes, each uniquely labeled from 0 to numNodes - 1. You are provided with a list of dependencies where each entry dependencies[i] = [sourceᵢ, targetᵢ] indicates that node sourceᵢ must be processed before node targetᵢ can be processed.
Dependencies exhibit transitive closure properties. This means if node A is a dependency of node B, and node B is a dependency of node C, then node A is implicitly a dependency of node C as well. In other words, to reach node C, you must have first visited node A.
You are also given a list of queries where each queries[j] = [uⱼ, vⱼ] asks: "Is node uⱼ reachable from node vⱼ through the dependency chain?" Equivalently, this asks whether uⱼ is a direct or indirect dependency of vⱼ.
Return a boolean array answer where answer[j] indicates whether node uⱼ is a dependency (direct or indirect) of node vⱼ for the jᵗʰ query.
numNodes = 2
dependencies = [[1,0]]
queries = [[0,1],[1,0]][false,true]The dependency [1, 0] indicates that node 1 must be processed before node 0. Therefore, node 1 is a dependency of node 0 (query [1,0] returns true), but node 0 is not a dependency of node 1 (query [0,1] returns false).
numNodes = 2
dependencies = []
queries = [[1,0],[0,1]][false,false]With no dependencies defined, all nodes are independent of each other. Neither node is a dependency of the other, so both queries return false.
numNodes = 3
dependencies = [[1,2],[1,0],[2,0]]
queries = [[1,0],[1,2]][true,true]Node 1 is a direct dependency of node 0 via [1,0]. Node 1 is also a direct dependency of node 2 via [1,2]. Additionally, node 1 is an indirect dependency of node 0 through node 2 (1 → 2 → 0). Both queries correctly return true.
Constraints