結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
👑 |
提出日時 | 2021-04-23 22:03:33 |
言語 | C (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 6 ms / 2,000 ms |
コード長 | 1,384 bytes |
コンパイル時間 | 264 ms |
コンパイル使用メモリ | 31,228 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-04 08:03:40 |
合計ジャッジ時間 | 1,435 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#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;}