Imagine you're building a universal unit converter for a scientific application! You have n different types of units (indexed from 0 to n-1), and you're given conversion relationships between them.
You receive a 2D array conversions where each element conversions[i] = [sourceUnit, targetUnit, conversionFactor] tells you that 1 unit of sourceUnit equals conversionFactor units of targetUnit.
Here's the challenge: Convert everything to a base unit system! You need to find how many units of each type are equivalent to 1 unit of type 0 (our base unit).
Since the conversion factors can get very large, return each result modulo 109 + 7.
Example: If 1 meter = 100 cm, and 1 cm = 10 mm, then 1 meter = 1000 mm. We want to express everything in terms of meters (unit 0).
Input & Output
Constraints
- 1 โค n โค 103
- conversions.length == n - 1 or fewer (tree or forest structure)
- 0 โค sourceUniti, targetUniti < n
- 1 โค conversionFactori โค 109
- All conversion factors are positive integers