LeetCode 399. Evaluate Division 有向并查集(被指向的作根) 拓扑排序

Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.

Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].

The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.

According to the example above:

equations = [ ["a", "b"], ["b", "c"] ],
values = [2.0, 3.0],
queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. 

 

The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.

----------------------------------------------------------

这题刚上来犯了个错,a/b=3,c/d=6,a/c=2,忘考虑a/c这层关系带来的大小放缩了。如果理解成一个拓扑排序,肯定是可以解的。评论区看到一种更好的有向并查集解法:a/b=3,那么并查集f[b]=(b,1),f[a]=(b,3);c/d=6,那么并查集f[d]=(d,1),f[c]=(d,6); a/c的时候,a原来指向b,而c是指向d的,所以f[b]=(d,6*2/3),其中3是从b到a的换算,所以是除3。

from collections import defaultdict

class Solution:
    def calcEquation(self, equations, values, queries):
        p = {}

        def find(x):
            if x not in p:
                p[x] = (x, 1.0)
            elif p[x][0] != x:
                v = p[x][1]
                pxt = find(p[x][0])
                pxn,pxv = pxt[0],pxt[1]
                p[x] = (pxn, v * pxv)
            return p[x]

        def union(x, y, v):
            pxt, pyt = find(x), find(y)
            pxn,pxv,pyn,pyv = pxt[0],pxt[1],pyt[0],pyt[1]
            if pxn != pyn:
                p[pxn] = (pyn, v * pyv / pxv)

        for (a, b), v in zip(equations, values):
            union(a, b, v)
            print(p)

        print('-------------------')
        ans = []
        for a, b in queries:
            res = p[a][1] / p[b][1] if (a in p and b in p and find(a)[0] == find(b)[0]) else -1.0
            ans.append(res)

        return ans

s = Solution()
print(s.calcEquation([["a","b"],["c","d"],["a","c"]],[3.0,6.0,2.0],[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]))
#print(s.calcEquation([["a","b"],["e","f"],["b","e"]],[3.4,1.4,2.3],[["b","a"],["a","f"],["f","f"],["e","e"],["c","c"],["a","c"],["f","e"]]))

其他方法,拓扑排序肯定没有问题;还有人直接DFS也过了的,说明测试数据很弱:

class Solution:
    def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
        def dfs(graph, start, end, visited):
            if start == end and graph[start]:
                return 1.0
            
            visited.add(start)
            for neigh, val in graph[start]:
                if neigh in visited:
                    continue
                
                tmp = dfs(graph, neigh, end, visited)
                if tmp > 0:
                    return val * tmp
            
            return -1.0
            
        graph = collections.defaultdict(set)
        for items, v in zip(equations, values):
            x, y = items
            graph[x].add((y, v))
            graph[y].add((x, 1.0 / v))
        
        res = []
        for q in queries:
            res.append(dfs(graph, q[0], q[1], set()))
        
        return res

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值