結果

問題 No.1488 Max Score of the Tree
ユーザー 小野寺健小野寺健
提出日時 2021-04-24 16:05:00
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,880 bytes
コンパイル時間 3,647 ms
コンパイル使用メモリ 74,824 KB
実行使用メモリ 110,188 KB
最終ジャッジ日時 2023-09-17 13:38:16
合計ジャッジ時間 11,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 147 ms
56,052 KB
testcase_06 WA -
testcase_07 AC 207 ms
77,244 KB
testcase_08 WA -
testcase_09 AC 197 ms
67,676 KB
testcase_10 AC 215 ms
83,716 KB
testcase_11 AC 254 ms
108,712 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 184 ms
67,052 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 233 ms
84,332 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 187 ms
67,748 KB
testcase_23 AC 126 ms
55,592 KB
testcase_24 AC 127 ms
55,924 KB
testcase_25 AC 133 ms
55,680 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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];
		for (int i=0; i < N-1; i++) {
			for (int w = 0; w <= K; w++) {
				if (w >= weight[i]) {
					dp[i+1][w] = Math.max(dp[i+1][w-weight[i]] + weight[i] * e[i], dp[i][w] + weight[i] * e[i]);
				} else {
					dp[i+1][w] = dp[i][w] + weight[i] * e[i];
				}
			}
		}
		System.out.println(dp[N-1][K]);
	}

}
0