博客
关于我
魔方俱乐部
阅读量:315 次
发布时间:2019-03-03

本文共 2423 字,大约阅读时间需要 8 分钟。

为了解决这个问题,我们需要找到每个分部从虫洞出发,通过虫洞遍历其他分部,使得访问的分部的或zFang价值之和最大的情况。每个分部只能通过虫洞通向一个特定的分部,这使得我们可以将问题分解为处理多个环和链结构。

方法思路

  • 问题分析:每个分部只能有一个出边,因此整个结构由多个环和链组成。环中的每个节点都可以访问整个环,而链结构则需要从终点逆向处理。
  • 递归DFS:使用深度优先搜索(DFS)来遍历每个节点,记录路径以检测环。对于环中的节点,计算环的总价值,并将每个节点的ans值设为这个总价值。对于链结构,从终点逆向处理。
  • 环处理:当在DFS中发现环时,计算环的总价值,并将环中的所有节点的ans值设为这个总价值。
  • 链处理:对于链结构,从终点开始逆向处理,计算每个节点的ans值。
  • 解决代码

    def main():    import sys    sys.setrecursionlimit(1 << 25)    n = int(sys.stdin.readline())    orz = list(map(int, sys.stdin.readline().split()))    F = list(map(int, sys.stdin.readline().split()))    F = [0] + F  # 1-based indexing    visited = [False] * (n + 1)    ans = [0] * (n + 1)    for i in range(1, n + 1):        if not visited[i]:            stack = []            current = i            while True:                if visited[current]:                    if current in stack:                        idx = stack.index(current)                        cycle = stack[idx:]                        sum_orz = sum(orz[node - 1] for node in cycle)                        for node in cycle:                            ans[node] = sum_orz                        for node in stack[:idx]:                            if F[node] == node:                                ans[node] = orz[node - 1]                            else:                                ans[node] = orz[node - 1] + ans[F[node]]                        break                    else:                        break                visited[current] = True                stack.append(current)                current = F[current]                if current in stack:                    idx = stack.index(current)                    cycle = stack[idx:]                    sum_orz = sum(orz[node - 1] for node in cycle)                    for node in cycle:                        ans[node] = sum_orz                    for node in stack[:idx]:                        if F[node] == node:                            ans[node] = orz[node - 1]                        else:                            ans[node] = orz[node - 1] + ans[F[node]]                    break    for i in range(1, n + 1):        print(ans[i])if __name__ == "__main__":    main()

    代码解释

  • 输入处理:读取输入数据,初始化或zFang价值数组和虫洞指向数组。
  • 初始化数组visited数组记录每个节点是否被访问过,ans数组记录每个节点的最大价值之和。
  • 遍历每个节点:对于每个未被访问的节点,使用非递归DFS遍历。
  • 检测环:在DFS过程中,检查是否形成环。如果形成环,计算环的总价值,并将环中的节点的ans值设为这个总价值。
  • 处理链结构:对于链结构,从终点开始逆向处理,计算每个节点的ans值。
  • 输出结果:输出每个节点的最大价值之和。
  • 这种方法确保了每个节点的ans值被正确计算,处理了环和链结构,保证了算法的高效性和正确性。

    转载地址:http://ntil.baihongyu.com/

    你可能感兴趣的文章
    Python - 递归和列表
    查看>>
    Python 3 读取ini文件
    查看>>
    Python 3.0 使用 turtle.onclick
    查看>>
    Python 3.10 明年发布,看看都有哪些新特性?
    查看>>
    python 3.10上安装pyqt5
    查看>>
    Python 3.12 正式发布了!
    查看>>
    Python 3.2 中的蛮力脚本
    查看>>
    Python 3.4 多处理递归 Pool.map()
    查看>>
    Python 3.4:未知格式代码“x“
    查看>>
    Python 3.5、ldap3 和 modify_password()
    查看>>
    python 3.6.8 升级至3.9版本升级
    查看>>
    Python 3.9 到 Python 3.12 的发展历程与区别
    查看>>
    python 32位和64位的区别在哪
    查看>>
    Python 3:何时使用 dict,何时使用元组列表?
    查看>>
    Python 3d 绘图 - 轴居中
    查看>>
    python ==》 字典
    查看>>
    python anaconda 安装使用
    查看>>
    python and或or 当参数传递的时候的用法
    查看>>
    Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?
    查看>>
    Python APP自动化测试工具adb与Monkey使用详解
    查看>>