class : defcalcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]: graph = {} for (x, y), v in zip(equations, values): if x in graph: graph[x][y] = v else: graph[x] = {y: v} if y in graph: graph[y][x] = 1 / v else: graph[y] = {x: 1 / v} def_dfs(s, t): if s notin graph: return-1 if t == s: return1 for node in graph[s].keys(): if node == t: return graph[s][node] elif node notin visited: visited.add(node) v = _dfs(node, t) if v != -1: return graph[s][node] * v return-1
res = [] for qs, qt in queries: visited = set() res.append(_dfs(qs, qt)) return res