結果

問題 No.1488 Max Score of the Tree
ユーザー SHIJOUSHIJOU
提出日時 2021-05-19 13:51:10
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 925 bytes
コンパイル時間 252 ms
コンパイル使用メモリ 31,616 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 00:06:12
合計ジャッジ時間 1,334 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<stdio.h>
#define N 110
#define K 100100
int pnt[N], G[N][N][2];
int dist[N];
int dp[K];
int dfs(int i, int j) {
  int res = 0;
  for(int k = 0; k < pnt[i]; k++) {
    if(G[i][k][0] == j) continue;
    int u = dfs(G[i][k][0], i);
    res += u + dist[G[i][k][0]]*G[i][k][1];
    dist[i] += dist[G[i][k][0]];
    for(int l = K-1; l >= G[i][k][1]; l--) {
      if(dp[l] < dp[l-G[i][k][1]] + G[i][k][1]*dist[G[i][k][0]]) {
        dp[l] = dp[l-G[i][k][1]] + G[i][k][1]*dist[G[i][k][0]];
      }
    }
  }
  dist[i] += !res;
  return res;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 0; i < n-1; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--; b--;
    G[a][pnt[a]][0] = b;
    G[a][pnt[a]++][1] = c;
    G[b][pnt[b]][0] = a;
    G[b][pnt[b]++][1] = c;
  }
  int ans = dfs(0, -1);
  int max = 0;
  for(int i = 0; i <= k; i++) if(max < dp[i]) max = dp[i];
  printf("%d\n", ans+max);
}
0