You are given an array of variable pairs equations and an array of real numbers values, where equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai and Bi is a string of lowercase letters. You are also given queries, where queries[j] = [Cj, Dj] asks for the value of Cj / Dj. Return the answers to all queries. If a single answer cannot be determined, return -1.0. Note: the input equations are always valid; no equation results in division by zero and there is no contradiction. In this catalog the values are chosen so every answer is an exact number. Variables not appearing in any equation give -1.0.
Example 1
Input: equations = [["a","b"],["b","c"]], values = [2.0,4.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["c","a"]]
Output: [8.0,0.5,-1.0,1.0,0.125]
Explanation: a/b=2, b/c=4 give a/c=8; unknown variable e yields -1; a/a=1.
Example 2
Input: equations = [["x","y"]], values = [4.0], queries = [["x","y"],["y","x"],["x","x"],["z","z"],["x","z"]]
Output: [4.0,0.25,1.0,-1.0,-1.0]
Explanation: z never appears, so any query involving z is -1.
Example 3
Input: equations = [["a","b"],["c","d"]], values = [2.0,8.0], queries = [["a","b"],["c","d"],["a","c"],["a","d"]]
Output: [2.0,8.0,-1.0,-1.0]
Explanation: The two equations form disconnected components, so a/c and a/d are undefined.
Read each equation A / B = k as a pair of directed edges in a graph: an edge A → B with weight k, and B → A with weight 1/k. Then C / D is just the product of edge weights along any path from C to D — because the intermediate variables cancel in a telescoping chain. Finding that product is a depth-first search from C to D.
Run checks all cases above. Submit evaluates all test cases.