A variation of **Floyd–Warshall**, computing quotients instead of shortest paths. An equation `A/B=k`

is like a graph edge `A->B`

, and `(A/B)*(B/C)*(C/D)`

is like the path `A->B->C->D`

. Submitted once, accepted in 35 ms.

```
def calcEquation(self, equations, values, queries):
quot = collections.defaultdict(dict)
for (num, den), val in zip(equations, values):
quot[num][num] = quot[den][den] = 1.0
quot[num][den] = val
quot[den][num] = 1 / val
for k, i, j in itertools.permutations(quot, 3):
if k in quot[i] and j in quot[k]:
quot[i][j] = quot[i][k] * quot[k][j]
return [quot[num].get(den, -1.0) for num, den in queries]
```

Variation without the `if`

(submitted twice, accepted in 68 and 39 ms):

```
def calcEquation(self, equations, values, queries):
quot = collections.defaultdict(dict)
for (num, den), val in zip(equations, values):
quot[num][num] = quot[den][den] = 1.0
quot[num][den] = val
quot[den][num] = 1 / val
for k in quot:
for i in quot[k]:
for j in quot[k]:
quot[i][j] = quot[i][k] * quot[k][j]
return [quot[num].get(den, -1.0) for num, den in queries]
```

Could save a line with `for i, j in itertools.permutations(quot[k], 2)`

but it's longer and I don't like it as much here.