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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值