結果
問題 | No.1488 Max Score of the Tree |
ユーザー | tenten |
提出日時 | 2022-08-17 19:47:23 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 337 ms / 2,000 ms |
コード長 | 2,761 bytes |
コンパイル時間 | 2,721 ms |
コンパイル使用メモリ | 85,348 KB |
実行使用メモリ | 89,760 KB |
最終ジャッジ日時 | 2024-10-04 23:56:22 |
合計ジャッジ時間 | 8,081 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 174 ms
87,288 KB |
testcase_01 | AC | 218 ms
85,324 KB |
testcase_02 | AC | 284 ms
86,780 KB |
testcase_03 | AC | 216 ms
89,056 KB |
testcase_04 | AC | 337 ms
89,448 KB |
testcase_05 | AC | 70 ms
50,012 KB |
testcase_06 | AC | 81 ms
58,832 KB |
testcase_07 | AC | 141 ms
74,620 KB |
testcase_08 | AC | 90 ms
62,192 KB |
testcase_09 | AC | 122 ms
61,048 KB |
testcase_10 | AC | 122 ms
74,768 KB |
testcase_11 | AC | 220 ms
89,760 KB |
testcase_12 | AC | 58 ms
50,388 KB |
testcase_13 | AC | 92 ms
56,828 KB |
testcase_14 | AC | 106 ms
76,068 KB |
testcase_15 | AC | 99 ms
63,148 KB |
testcase_16 | AC | 58 ms
52,132 KB |
testcase_17 | AC | 84 ms
55,696 KB |
testcase_18 | AC | 153 ms
82,708 KB |
testcase_19 | AC | 97 ms
62,864 KB |
testcase_20 | AC | 84 ms
55,556 KB |
testcase_21 | AC | 58 ms
52,060 KB |
testcase_22 | AC | 100 ms
63,708 KB |
testcase_23 | AC | 57 ms
50,432 KB |
testcase_24 | AC | 58 ms
50,336 KB |
testcase_25 | AC | 58 ms
50,024 KB |
testcase_26 | AC | 64 ms
59,364 KB |
testcase_27 | AC | 59 ms
50,316 KB |
testcase_28 | AC | 87 ms
55,860 KB |
testcase_29 | AC | 90 ms
56,056 KB |
testcase_30 | AC | 118 ms
81,384 KB |
testcase_31 | AC | 260 ms
89,712 KB |
ソースコード
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(); } }