結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 ygussanyygussany
提出日時 2021-04-23 21:44:56
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,404 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 30,612 KB
実行使用メモリ 81,576 KB
最終ジャッジ日時 2023-09-17 11:58:05
合計ジャッジ時間 2,465 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 21 ms
81,044 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 31 ms
81,476 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 21 ms
80,980 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 38 ms
81,416 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 22 ms
80,668 KB
testcase_24 AC 22 ms
81,128 KB
testcase_25 AC 23 ms
80,768 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

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

void chmax(long long* a, long long 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;
			}
		}
	}

	long long dp[101][100001] = {}, tmp[100001], leaf[101] = {};
	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];
			for (i = 0; i <= K; i++) tmp[i] = dp[w][i];
			for (i = c; i <= K; i++) chmax(&(tmp[i]), dp[w][i-c] + c * leaf[w]);
			for (i = 0; i <= K; i++) dp[u][i] += tmp[i];
		}
		if (leaf[u] == 0) {
			leaf[u] = 1;
			dp[u][0] = depth[u];
		}
	}
	
	long long ans = 0;
	for (i = 0; i <= K; i++) chmax(&ans, dp[1][i]);
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0