結果

問題 No.1488 Max Score of the Tree
ユーザー 小野寺健小野寺健
提出日時 2021-04-24 16:20:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 257 ms / 2,000 ms
コード長 1,887 bytes
コンパイル時間 3,578 ms
コンパイル使用メモリ 79,660 KB
実行使用メモリ 108,512 KB
最終ジャッジ日時 2023-09-17 13:38:41
合計ジャッジ時間 11,181 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
108,172 KB
testcase_01 AC 245 ms
90,928 KB
testcase_02 AC 250 ms
108,512 KB
testcase_03 AC 252 ms
108,068 KB
testcase_04 AC 254 ms
108,244 KB
testcase_05 AC 147 ms
55,616 KB
testcase_06 AC 172 ms
64,996 KB
testcase_07 AC 207 ms
77,696 KB
testcase_08 AC 188 ms
70,976 KB
testcase_09 AC 196 ms
67,680 KB
testcase_10 AC 215 ms
83,680 KB
testcase_11 AC 257 ms
108,336 KB
testcase_12 AC 130 ms
55,712 KB
testcase_13 AC 184 ms
60,544 KB
testcase_14 AC 199 ms
77,316 KB
testcase_15 AC 186 ms
67,264 KB
testcase_16 AC 150 ms
58,088 KB
testcase_17 AC 166 ms
64,724 KB
testcase_18 AC 226 ms
84,488 KB
testcase_19 AC 189 ms
67,832 KB
testcase_20 AC 174 ms
60,372 KB
testcase_21 AC 151 ms
57,972 KB
testcase_22 AC 189 ms
67,660 KB
testcase_23 AC 128 ms
55,948 KB
testcase_24 AC 128 ms
56,008 KB
testcase_25 AC 134 ms
55,656 KB
testcase_26 AC 167 ms
64,684 KB
testcase_27 AC 155 ms
58,004 KB
testcase_28 AC 167 ms
60,616 KB
testcase_29 AC 168 ms
60,396 KB
testcase_30 AC 225 ms
86,496 KB
testcase_31 AC 253 ms
108,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;

public class No1488 {
	
	private static class Peek {
		List<Integer> edge;
		List<Integer[]> peeks;
		public Peek() {
			edge = new ArrayList<Integer>();
			peeks = new ArrayList<Integer[]>();
		}
	}
	
	private static class Queue {
		int i;
		List<Integer> edge;
		public Queue(int i) {
			this.i = i;
			this.edge = new ArrayList<Integer>();
		}
		public Queue(int i, List<Integer> edge, int c) {
			this.i = i;
			this.edge = new ArrayList<Integer>(edge);
			this.edge.add(c);
		}
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int K = scan.nextInt();
		List<Peek> v = new ArrayList<Peek>();
		for (int i=0; i < N; i++) {
			v.add(new Peek());
		}
		int[] weight = new int[N-1];
		for (int i=0; i < N-1; i++) {
			int a = scan.nextInt() - 1;
			int b = scan.nextInt() - 1;
			int c = scan.nextInt();
			v.get(a).peeks.add(new Integer[] {b, i});
			v.get(b).peeks.add(new Integer[] {a, i});
			weight[i] = c;
		}
		scan.close();
		List<Queue> q = new ArrayList<Queue>();
		q.add(new Queue(0));
		int[] e = new int[N-1];
		while (q.size() > 0) {
			Queue qi = q.remove(0);
			v.get(qi.i).edge = qi.edge;
			boolean cnt = true;
			for (Integer[] j : v.get(qi.i).peeks) {
				if (j[0] == 0 || v.get(j[0]).edge.size() > 0) {
					continue;
				}
				cnt = false;
				q.add(new Queue(j[0], qi.edge, j[1]));
			}
			if (cnt) {
				for (int j : qi.edge) {
					e[j]++;
				}
			}
		}
		int[][] dp = new int[N][K+1];
		int val = 0;
		for (int i=0; i < N-1; i++) {
			val += weight[i] * e[i];
			for (int w = 0; w <= K; w++) {
				if (w >= weight[i]) {
					dp[i+1][w] = Math.max(dp[i][w-weight[i]] + weight[i] * e[i], dp[i][w]);
				} else {
					dp[i+1][w] = dp[i][w];
				}
			}
		}
		System.out.println(dp[N-1][K]+val);
	}

}
0