当前位置: 首页 > news >正文

OJ-0710

示例1

input
4
100 200 300 5001 21 32 4output700
  • 100
    • 200
      • 500
    • 300

示例2

input
4
100 200 300 500
1 2
1 3
1 4output1100
  • 100
    • 200
    • 500
    • 300

示例3

input
6
100 200 300 400 300 550
1 2
1 3
1 4
2 5
2 6output1050
  • 100
    • 200
      • 300
      • 600
    • 300
    • 400
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int N = scanner.nextInt();int[] val = new int[N + 1];for (int i = 1; i <= N; ++i) {val[i] = scanner.nextInt();}Map<Integer, List<Integer>> mp = new HashMap<>();for (int i = 0; i < N - 1; ++i) {int x1 = scanner.nextInt();int x2 = scanner.nextInt();mp.computeIfAbsent(x1, k -> new ArrayList<>()).add(x2);}TreeNode root = new TreeNode(val[1]);for (Map.Entry<Integer, List<Integer>> entry : mp.entrySet()) {TreeNode parent = new TreeNode(val[entry.getKey()]);for (int childId : entry.getValue()) {parent.children.add(new TreeNode(val[childId]));}root.children.add(parent);}int max_wealth = 0;dfs(root, max_wealth);System.out.println(max_wealth);}private static int dfs(TreeNode node, int max_wealth) {int current_wealth = node.value;for (TreeNode child : node.children) {current_wealth += dfs(child, max_wealth);}max_wealth = Math.max(max_wealth, current_wealth);return current_wealth;}
}class TreeNode {int value;List<TreeNode> children;public TreeNode(int value) {this.value = value;this.children = new ArrayList<>();}
}

https://blog.csdn.net/weixin_52908342/article/details/135189680

http://www.lryc.cn/news/397207.html

相关文章:

  • 人工智能在自动驾驶中的目标检测研究
  • 【合并两个有序数组】
  • 链表 OJ(一)
  • 《Linux与Windows文件系统的区别》
  • 批量修改Git历史commit信息中的username
  • LabVIEW与ABB工业机器人据监控
  • c++栈内存和堆内存的基本使用
  • 快速入门,springboot知识点汇总
  • Ubuntu20.04系统非root用户安装GAMIT10.71
  • stm32 开发板可以拿来做什么?
  • latex英文转中文word,及一些latex相关工具分享
  • EasyOCR: 简单易用的多语言OCR工具
  • arm架构安装chrome
  • ETAS工具导入Com Arxml修改步骤
  • Apache Kylin模型构建全解析:深入理解大数据的多维分析
  • element-plus的文件上传组件el-upload
  • 等保测评视角下的哈尔滨智慧城市安全框架构建
  • Java中的数据缓存技术及其应用
  • SQL 索引
  • free第一次成功,第二次失败
  • 各种音频处理器
  • 深度学习探秘:Transformer模型跨框架实现大比拼
  • 京准电钟:云计算中NTP网络时间服务器的作用是什么?
  • Apache中使用CGI
  • 宏任务与微任务对比【前端异步】
  • Autogen和LangGraph对比
  • uniapp vue3微信小程序如何获取dom元素
  • Mongodb索引使用限制
  • 阿里云通义千问开源两款语音基座模型分别是SenseVoice和CosyVoice
  • 第11章 规划过程组(二)(11.10制订进度计划)