← 返回题库
中级

最小生成树扩展

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve(____):
    from collections import defaultdict
    edges_list = []
    for e in edges.split(____):
        u, v, w = map(int, e.split(____))
        edges_list.append((____))
    edges_list.sort()
    parent = list(range(____))
    def find(____):
        if parent[x] != x:
            parent[x] = find(____)
        return parent[x]
    total = ____
    for w, u, v in edges_list:
        pu, pv = find(____), find(____)
        if pu != pv:
            parent[pu] = pv
            total += w
    print(____)

示例

输入
3|3|1,2,1;2,3,2;1,3,4
期望输出
3
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码