Unit Conversion II Problem
Description
LeetCode Problem 3535.
There are n types of units indexed from 0 to n - 1.
You are given a 2D integer array conversions of length n - 1, where conversions[i] = [sourceUnit_i, targetUnit_i, conversionFactor_i]. This indicates that a single unit of type sourceUnit_i is equivalent to conversionFactor_i units of type targetUnit_i.
You are also given a 2D integer array queries of length q, where queries[i] = [unitA_i, unitB_i].
Return an array answer of length q where answer[i] is the number of units of type unitB_i equivalent to 1 unit of type unitA_i, and can be represented as p/q where p and q are coprime. Return each answer[i] as pq^(-1) modulo 10^9 + 7, where q^(-1) represents the multiplicative inverse of q modulo 10^9 + 7.
Example 1:
Input: conversions = [[0,1,2],[0,2,6]], queries = [[1,2],[1,0]]
Output: [3,500000004]
Explanation:
- In the first query, we can convert unit 1 into 3 units of type 2 using the inverse of conversions[0], then conversions[1].
- In the second query, we can convert unit 1 into 1/2 units of type 0 using the inverse of conversions[0]. We return 500000004 since it is the multiplicative inverse of 2.

Example 2:
Input: conversions = [[0,1,2],[0,2,6],[0,3,8],[2,4,2],[2,5,4],[3,6,3]], queries = [[1,2],[0,4],[6,5],[4,6],[6,1]]
Output: [3,12,1,2,83333334]
Explanation:
- In the first query, we can convert unit 1 into 3 units of type 2 using the inverse of conversions[0], then conversions[1].
- In the second query, we can convert unit 0 into 12 units of type 4 using conversions[1], then conversions[3].
- In the third query, we can convert unit 6 into 1 unit of type 5 using the inverse of conversions[5], the inverse of conversions[2], conversions[1], then conversions[4].
- In the fourth query, we can convert unit 4 into 2 units of type 6 using the inverse of conversions[3], the inverse of conversions[1], conversions[2], then conversions[5].
- In the fifth query, we can convert unit 6 into 1/12 units of type 1 using the inverse of conversions[5], the inverse of conversions[2], then conversions[0]. We return 83333334 since it is the multiplicative inverse of 12.

Constraints:
2 <= n <= 10^5conversions.length == n - 10 <= sourceUnit_i, targetUnit_i < n1 <= conversionFactor_i <= 10^91 <= q <= 10^5queries.length == q0 <= unitA_i, unitB_i < n- It is guaranteed that unit 0 can be uniquely converted into any other unit through a combination of forward or backward conversions.
Solution
Approach: DFS + Modular Inverse
The conversions form a tree rooted at 0. A DFS from 0 computes res[i], the number of units of type i equal to one unit of type 0, modulo 10^9 + 7. For a query (unitA, unitB) the answer is res[unitB] / res[unitA], which under the prime modulus p = 10^9 + 7 is res[unitB] * res[unitA]^(p - 2) mod p by Fermat's little theorem.
Complexity: O(n + q log p) time and O(n) extra space, where n is the number of units, q the number of queries and p the modulus.
class Solution:
def queryConversions(
self, conversions: List[List[int]], queries: List[List[int]]
) -> List[int]:
def dfs(s: int, mul: int) -> None:
res[s] = mul
for t, w in g[s]:
dfs(t, mul * w % mod)
mod = 10**9 + 7
n = len(conversions) + 1
g = [[] for _ in range(n)]
for s, t, w in conversions:
g[s].append((t, w))
res = [0] * n
dfs(0, 1)
ans = []
for x, y in queries:
ans.append(res[y] * pow(res[x], mod - 2, mod) % mod)
return ans