結果

問題 No.1488 Max Score of the Tree
ユーザー tentententen
提出日時 2022-08-17 19:47:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 2,761 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 80,936 KB
実行使用メモリ 89,528 KB
最終ジャッジ日時 2024-04-15 08:04:47
合計ジャッジ時間 7,077 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
86,292 KB
testcase_01 AC 178 ms
85,900 KB
testcase_02 AC 224 ms
87,736 KB
testcase_03 AC 168 ms
88,620 KB
testcase_04 AC 229 ms
88,752 KB
testcase_05 AC 58 ms
50,348 KB
testcase_06 AC 69 ms
59,024 KB
testcase_07 AC 109 ms
74,892 KB
testcase_08 AC 70 ms
62,548 KB
testcase_09 AC 102 ms
61,444 KB
testcase_10 AC 107 ms
74,512 KB
testcase_11 AC 170 ms
88,812 KB
testcase_12 AC 55 ms
50,412 KB
testcase_13 AC 92 ms
57,032 KB
testcase_14 AC 108 ms
76,112 KB
testcase_15 AC 94 ms
63,248 KB
testcase_16 AC 56 ms
52,396 KB
testcase_17 AC 85 ms
56,340 KB
testcase_18 AC 146 ms
82,596 KB
testcase_19 AC 94 ms
63,700 KB
testcase_20 AC 83 ms
55,392 KB
testcase_21 AC 58 ms
52,172 KB
testcase_22 AC 96 ms
64,148 KB
testcase_23 AC 55 ms
50,432 KB
testcase_24 AC 55 ms
50,044 KB
testcase_25 AC 56 ms
50,156 KB
testcase_26 AC 62 ms
59,144 KB
testcase_27 AC 58 ms
50,276 KB
testcase_28 AC 86 ms
55,800 KB
testcase_29 AC 88 ms
56,384 KB
testcase_30 AC 115 ms
81,708 KB
testcase_31 AC 209 ms
89,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static Node[] nodes;
    static int[][] dp;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        for (int i = 0; i < n; i++) {
            graph.add(new HashMap<>());
        }
        nodes = new Node[n - 1];
        for (int i = 0; i < n - 1; i++) {
            nodes[i] = new Node(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextInt());
            graph.get(nodes[i].left).put(nodes[i].right, i);
            graph.get(nodes[i].right).put(nodes[i].left, i);
        }
        setCounts(0, 0);
        dp = new int[n - 1][k + 1];
        System.out.println(dfw(n - 2, k));
    }
    
    static int setCounts(int idx, int p) {
        int count = 0;
        for (int x : graph.get(idx).keySet()) {
            if (x == p) {
                continue;
            }
            count += setCounts(x, idx);
        }
        if (count == 0) {
            count = 1;
        }
        if (idx != p) {
            nodes[graph.get(idx).get(p)].count = count;
        }
        return count;
    }
    
    static int dfw(int idx, int v) {
        if (v < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][v] == 0) {
            dp[idx][v] = Math.max(dfw(idx - 1, v) + nodes[idx].getSum(), dfw(idx - 1, v - nodes[idx].score) + nodes[idx].getSum() * 2);
        }
        return dp[idx][v];
    }
    
    static class Node {
        int left;
        int right;
        int score;
        int count;
        
        public Node(int left, int right, int score) {
            this.left = left;
            this.right = right;
            this.score = score;
        }
        
        public int getSum() {
            return score * count;
        }
        
        public String toString() {
            return score + ":" + count;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0