⚠️ 换一张图,比值正好顶到 2

👁️ 0 人浏览 💬 0 人评论 ❤️ 添加收藏

换一张图:三条互不相连的边 [(0, 1), (2, 3), (4, 5)]。贪心和最优各要几个点?

from itertools import combinations

E2 = [(0, 1), (2, 3), (4, 5)]
V2 = [0, 1, 2, 3, 4, 5]

def greedy_vc(edges):
    cover, used = [], set()
    for a, b in edges:
        if a not in used and b not in used:
            cover += [a, b]
            used |= {a, b}
    return sorted(cover)

def opt_vc(edges, vs):
    for k in range(len(vs) + 1):
        for c in combinations(vs, k):
            s = set(c)
            if all(a in s or b in s for a, b in edges):
                return sorted(s)
    return sorted(vs)

print(str(len(greedy_vc(E2))) + "/" + str(len(opt_vc(E2, V2))))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论