OJ-0710

示例1

input
4
100 200 300 500
1 2
1 3
2 4
output
700
  • 100
    • 200
      • 500
    • 300

示例2

input
4
100 200 300 500
1 2
1 3
1 4
 output
 1100
  • 100
    • 200
    • 500
    • 300

示例3

input
6
100 200 300 400 300 550
1 2
1 3
1 4
2 5
2 6
 output
 1050
  • 100
    • 200
      • 300
      • 600
    • 300
    • 400

题解:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int[] money = new int[num];
        for (int i = 0; i < num; i++) {
            money[i] = in.nextInt();
        }
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < num - 1; i++) {
            int parent = in.nextInt();
            int child = in.nextInt();
            map.put(parent, map.getOrDefault(parent, money[parent - 1]) + money[child - 1]);
        }
        int maxMoney = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            maxMoney = Math.max(entry.getValue(), maxMoney);
        }
        System.out.println(maxMoney);
    }
}

参考

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

相关推荐

  1. OJ-0710

    2024-07-14 22:02:01       21 阅读
  2. OJ-0718

    2024-07-14 22:02:01       22 阅读
  3. 071107120713 进程,进程之间的通信

    2024-07-14 22:02:01       17 阅读
  4. <span style='color:red;'>0110</span>qt

    0110qt

    2024-07-14 22:02:01      55 阅读
  5. Linux<span style='color:red;'>0715</span>

    Linux0715

    2024-07-14 22:02:01      28 阅读
  6. 010-$nextTick

    2024-07-14 22:02:01       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 22:02:01       101 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 22:02:01       109 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 22:02:01       87 阅读
  4. Python语言-面向对象

    2024-07-14 22:02:01       96 阅读

热门阅读

  1. throw 和throws的区别详解

    2024-07-14 22:02:01       21 阅读
  2. Springboot中Aop的使用案列

    2024-07-14 22:02:01       22 阅读
  3. 计算机网络——常见问题汇总2

    2024-07-14 22:02:01       21 阅读
  4. helm系列之-使用helm部署应用

    2024-07-14 22:02:01       22 阅读
  5. A. Only Pluses

    2024-07-14 22:02:01       24 阅读
  6. 质量小议40 -- 合格

    2024-07-14 22:02:01       23 阅读
  7. IOS热门面试题(二)

    2024-07-14 22:02:01       23 阅读
  8. KVM-QEMU

    KVM-QEMU

    2024-07-14 22:02:01      16 阅读
  9. Linux重要知识点

    2024-07-14 22:02:01       20 阅读