結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 ygussanyygussany
提出日時 2021-04-23 22:03:33
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,384 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 30,224 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 12:16:24
合計ジャッジ時間 1,846 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,376 KB
testcase_01 AC 5 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,384 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 4 ms
4,380 KB
testcase_31 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

typedef struct List {
	struct List *next;
	int v, cost;
} list;

void chmax(int* a, int b)
{
	if (*a < b) *a = b;
}

int main()
{
	int i, N, K, u, w, c;
	list *adj[101] = {}, e[201], *p;
	scanf("%d %d", &N, &K);
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d %d", &u, &w, &c);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].cost = c;
		e[i*2+1].cost = c;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int par[101] = {}, depth[101], q[101], head, tail;
	par[1] = 1;
	depth[1] = 0;
	q[0] = 1;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] == 0) {
				par[w] = u;
				depth[w] = depth[u] + p->cost;
				q[tail++] = w;
			}
		}
	}

	int leaf[101] = {}, weight[101], value[101], sum = 0;
	for (head--; head >= 0; head--) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			c = p->cost;
			if (w == par[u]) continue;
			leaf[u] += leaf[w];
			weight[w] = c;
			value[w] = leaf[w] * c;
		}
		if (leaf[u] == 0) {
			sum += depth[u];
			leaf[u] = 1;
		}
	}
	
	int ans = 0, dp[100001] = {};
	for (u = 2; u <= N; u++) {
		for (i = K; i >= weight[u]; i--) chmax(&(dp[i]), dp[i - weight[u]] + value[u]);
	}
	for (i = 0; i <= K; i++) chmax(&ans, dp[i]);
	printf("%d\n", ans + sum);
	fflush(stdout);
	return 0;
}
0