博客
关于我
魔方俱乐部
阅读量: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--欧拉习题二
    查看>>
    python--高级应用总结
    查看>>
    python-520表白代码,我看谁说程序员不懂浪漫,送你几套表白代码,一步到位
    查看>>
    Python-Asyncio TypeError:无法在‘;等待‘;表达式中使用对象DICT
    查看>>
    Python-AttributeError:‘;dict&39;对象没有属性‘;序列‘;
    查看>>
    Python-ldap:是否可以在不显式写入密码的情况下进行绑定?
    查看>>
    python出现 unknown encoding: idna 的处理方法
    查看>>
    Python出现 RuntimeError: dictionary changed size during iteration 的解决方法
    查看>>
    Python-numpy(2)数组的操作
    查看>>
    Python-Numpy的tile函数用法
    查看>>
    python-open函数
    查看>>
    Python-Pandas-将特定函数应用于给定级别-多索引数据框架
    查看>>
    python-queue队列通信
    查看>>
    python-ros No module named PyKDL
    查看>>
    python-social-auth 是否需要会话
    查看>>
    Python再获年度编程语言,微软或成最大赢家
    查看>>
    python-week5
    查看>>
    Python写表情包搜索工具(非常详细)零基础入门到精通,收藏这一篇就够了
    查看>>
    Python-【1】配置
    查看>>