Unit conversions from known facts
Each fact [a, b, k] says that one a equals k units of b, where k is a positive number, not always whole. Write convert(facts, queries), which returns one answer per query, in order: for [x, y], how many y equal one x, chaining facts in either direction, or -1.0 if the facts don't determine it. A unit that appears in no fact is unknown, even in [x, x].
The facts never contradict each other. Aim for near-constant time per query, not a new search each time. The tests call the provided run_conversions(facts, queries), which rounds your answers to 6 decimals, so floating-point noise doesn't matter.
Besides the tests, one large input (2,000 units, 2,000 queries) with a ~300 ms limit (scaled to your device's speed): brute force won't pass.
Examples
run_conversions([["km","m",1000],["m","cm",100]], [["km","cm"],["cm","km"],["m","m"],["km","mile"]])→[100000,0.00001,1,-1]run_conversions([["hour","minute",60],["day","hour",24],["kg","g",1000]], [["day","minute"],["minute","hour"],["day","kg"],["g","kg"]])→[1440,0.016667,-1,0.001]run_conversions([["foot","inch",12]], [["inch","foot"],["yard","yard"],["foot","foot"]])→[0.083333,-1,1]