博客
关于我
魔方俱乐部
阅读量: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/

    你可能感兴趣的文章
    node exporter完整版
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
    查看>>
    Node 裁切图片的方法
    查看>>
    Node+Express连接mysql实现增删改查
    查看>>
    node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
    查看>>
    Node-RED中Button按钮组件和TextInput文字输入组件的使用
    查看>>
    Node-RED中Switch开关和Dropdown选择组件的使用
    查看>>
    Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>
    Node-RED中使用node-red-contrib-image-output节点实现图片预览
    查看>>
    Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
    查看>>
    Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
    查看>>
    Node-RED中使用range范围节点实现从一个范围对应至另一个范围
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node-RED中将CSV数据写入txt文件并从文件中读取解析数据
    查看>>
    Node-RED中建立TCP服务端和客户端
    查看>>