Problem of the day
You are given an array of pairs of strings 'EQUATIONS', and an array of real numbers 'VALUES'. Each element of the 'EQUATIONS' array denotes a fraction where the first string denotes the numerator variable and the second string denotes the denominator variable, and the corresponding element in 'VALUES' denotes the value this fraction is equal to.
You are given ‘Q’ queries, and each query consists of two strings representing the numerator and the denominator of a fraction. You have to return the value of the given fraction for each query. Return -1 if the value cannot be determined.
Example :'EQUATIONS' = { {“a”, ”s”} , {“s”, “r”} }
'VALUES' = { 1.5, 2 }
queries = { {“a”, “r” } }
For the above example (a / s) = 1.5 and (s / r) = 2 therefore (a / r) = 1.5 * 2 = 3.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains two integers, ‘N’ and ‘Q,’ separated by a single space denoting the number of the equations and the number of queries, respectively.
The second line of each test case contains ‘N’ strings denoting the numerator variable of the 'EQUATIONS'.
The third line of each test case contains ‘N’ strings denoting the denominator variable of the 'EQUATIONS'.
The fourth line of each test case contains ‘N’ real numbers denoting the 'VALUE' of each fraction.
The fifth line of each test case contains ‘Q’ strings denoting the numerator variable for each query.
The sixth line of each test case contains ‘Q’ strings denoting the denominator variable for each query.
Output Format :
For each test case, return the value of the fraction up to 5 decimal places or -1 if the value of the fraction cannot be determined. All values are separated by a single space.
Your output will be considered correct if the relative error does not exceed 10^(-6).
Note :
You don’t need to print anything, It has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 100
1 <= Q <= 100
1 <= |S| <= 10
0.0 < values[i] <= 100.0
Where '|S|' denotes the length of the variables, and 'VALUES[i]' denotes the value of the i’th equation.
Time limit: 1 sec
2
2 1
a s
s r
1.5 2
a
r
3 2
a abc ab
x xyz xy
0.5 1 3.4
abc pqr
xyz rew
3.00000
1.00000 -1.00000
In test case 1, (a / s) = 1.5 and (s / r) = 2 therefore (a / r) = 1.5 * 2 = 3.
In test case 2, for the first query, the value of (abc / xyz) is given as 1, and the value of (pqr / rew) cannot be determined.
2
4 2
a r w p
r w p e
1.2 2.6 1 0.5
e a
p p
2 1
a x
b y
0.5 0.4
a
y
2.00000 3.12000
-1.00000
In test case 1, for the first query we have p / e = 0.5 ,therefore e / p = 1 / 0.5 = 2, for the second query (a / r) * (r / w) * (w / p) = a / p which is equal to 1.2 * 2.6 * 1 = 3.12.
In test case 2, we can not determine the value of the a / y, by the given set of equations. Thus return -1.
Can you think about representing the equations by a directed graph?
The idea is to construct a weighted directed graph from the given equations. For each given equation, let’s make an edge from the numerator variable to the denominator variable with weight equal to the value of the equation and another edge from the denominator to the numerator with weight equal to the inverse of the value of the equation. After making the graph to solve any query, we will need to find a path from the numerator to the denominator.
Let’s consider any two nodes in the graph ‘A’ and ‘B, then the path from ‘A’ to ‘B’ will denote the fraction ‘A' / ‘B’, and its value will be equal to the multiplication of the weights of all edges on the path from ‘A’ to ‘B’.
Let’s consider the following path from ‘A’ to ‘B’ (A -> X -> Y -> B); we know that each directed of the form P->Q represents a fraction P/Q. Hence the value of this path will be (A / X) * (X / Y) * (Y / B) = A / B.
The steps are as follows :
O(N * Q), Where ‘N’ is the number of equations, and ‘Q’ is the number of queries.
Since we are answering Q queries, and for each query, we are traversing the graph which will have N nodes and a total of 2 * N edges thus the total time taken to traverse the graph will be (N + 2 * N) in the worst case. Thus, the overall time complexity is O(N * Q).
O(N), Where 'N' is the number of equations.
Since we are creating a graph in which we are representing each equation with an edge. Since there are N equations storing the graph will take O(N) space. Thus, the overall space complexity is O(N).