結果

問題 No.1488 Max Score of the Tree
ユーザー tentententen
提出日時 2021-05-07 09:05:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 2,481 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 75,212 KB
実行使用メモリ 108,164 KB
最終ジャッジ日時 2023-10-13 00:14:51
合計ジャッジ時間 7,451 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
86,476 KB
testcase_01 AC 182 ms
86,444 KB
testcase_02 AC 277 ms
88,848 KB
testcase_03 AC 237 ms
108,088 KB
testcase_04 AC 269 ms
105,820 KB
testcase_05 AC 48 ms
49,232 KB
testcase_06 AC 73 ms
60,028 KB
testcase_07 AC 104 ms
74,248 KB
testcase_08 AC 79 ms
64,088 KB
testcase_09 AC 87 ms
60,524 KB
testcase_10 AC 99 ms
74,372 KB
testcase_11 AC 241 ms
107,940 KB
testcase_12 AC 44 ms
49,336 KB
testcase_13 AC 81 ms
55,840 KB
testcase_14 AC 111 ms
76,184 KB
testcase_15 AC 81 ms
64,352 KB
testcase_16 AC 45 ms
51,316 KB
testcase_17 AC 72 ms
56,216 KB
testcase_18 AC 159 ms
82,992 KB
testcase_19 AC 81 ms
63,740 KB
testcase_20 AC 79 ms
56,380 KB
testcase_21 AC 46 ms
51,292 KB
testcase_22 AC 86 ms
64,732 KB
testcase_23 AC 45 ms
49,324 KB
testcase_24 AC 45 ms
49,464 KB
testcase_25 AC 47 ms
49,408 KB
testcase_26 AC 54 ms
58,212 KB
testcase_27 AC 48 ms
49,268 KB
testcase_28 AC 74 ms
55,648 KB
testcase_29 AC 76 ms
56,108 KB
testcase_30 AC 107 ms
81,272 KB
testcase_31 AC 248 ms
108,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Path>> graph = new ArrayList<>();
    static ArrayList<Path> paths = new ArrayList<>();
    static int total = 0;
    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 ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            graph.get(a).add(new Path(b, c));
            graph.get(b).add(new Path(a, c));
        }
        getChildren(0, 0, 0);
        dp = new int[n - 1][k + 1];
        System.out.println(dfw(n - 2, k) + total);
    }
    
    static int dfw(int idx, int cost) {
        if (cost < 0) {
            return Integer.MIN_VALUE;
        }
        if (idx < 0) {
            return 0;
        }
        if (dp[idx][cost] == 0) {
            Path p = paths.get(idx);
            dp[idx][cost] = Math.max(dfw(idx - 1, cost), dfw(idx - 1, cost - p.value) + p.count * p.value);
        }
        return dp[idx][cost];
    }
    
    static int getChildren(int idx, int parent, int value) {
        int sum = 0;
        for (Path p : graph.get(idx)) {
            if (p.idx == parent) {
                continue;
            }
            p.count = getChildren(p.idx, idx, value + p.value);
            sum += p.count;
            paths.add(p);
        }
        if (sum == 0) {
            total += value;
            sum++;
        }
        return sum;
    }
    
    static class Path {
        int idx;
        int value;
        int count;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
    }
}

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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0