博客
关于我
魔方俱乐部
阅读量: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 numba 转灰度图_使用NumPy、Numba的简单使用(二)
    查看>>
    Python Numpy 关于 linspace()函数 使用详解(全)
    查看>>
    Python numpy插入、读取至postgreSQL数据库中bytea类型字段
    查看>>
    Python numpy数据的保存和读取
    查看>>
    python numpy矩阵索引_python – 在2D numpy ndarray或numpy矩阵中获取前N个值的索引
    查看>>
    Python OCR库:自动化测试验证码识别神器!
    查看>>
    python opencv - 斑点检测或圆形检测
    查看>>
    Python还可以做情感分析你不看看吗?
    查看>>
    Python OpenCV HoughLinesP 无法检测线
    查看>>
    Python OpenCV-使用透明度覆盖图像
    查看>>
    python Opencv图像基础操作
    查看>>
    Python OpenCV将图像转换为字节字符串?
    查看>>
    python OpenCV视频的读取及保存
    查看>>
    python open和file的区别
    查看>>
    python open读取文件并对换行进行处理
    查看>>
    python os, os.path和sys模块
    查看>>
    Python os.environ 处理环境变量
    查看>>
    python os.path模块常用方法详解
    查看>>
    python os.system
    查看>>
    Python os.system执行多条语句,os.system的返回值以及与os.popen的区别
    查看>>