結果
| 問題 |
No.1488 Max Score of the Tree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-06-27 23:01:45 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
AC
|
| 実行時間 | 109 ms / 2,000 ms |
| コード長 | 1,781 bytes |
| コンパイル時間 | 15,113 ms |
| コンパイル使用メモリ | 170,272 KB |
| 実行使用メモリ | 193,680 KB |
| 最終ジャッジ日時 | 2024-06-27 23:02:04 |
| 合計ジャッジ時間 | 19,378 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (95 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
static int NN => int.Parse(ReadLine());
static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
public static void Main()
{
Solve();
}
static void Solve()
{
var p = NList;
var (n, k) = (p[0], p[1]);
var map = NArr(n - 1);
var tree = new List<(int id, int to)>[n];
for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int id, int to)>();
for (var i = 0; i < map.Length; ++i)
{
tree[map[i][0] - 1].Add((i, map[i][1] - 1));
tree[map[i][1] - 1].Add((i, map[i][0] - 1));
}
var mul = new int[n - 1];
DFS(0, -1, tree, mul);
var dp = new long[k + 1];
var ans = 0L;
for (var i = 0; i < map.Length; ++i)
{
ans += map[i][2] * mul[i];
var ndp = (long[]) dp.Clone();
for (var j = 0; j < dp.Length && j + map[i][2] <= k; ++j)
{
ndp[j + map[i][2]] = Math.Max(ndp[j + map[i][2]], dp[j] + map[i][2] * mul[i]);
}
dp = ndp;
}
WriteLine(ans + dp.Max());
}
static int DFS(int cur, int prev, List<(int id, int to)>[] tree, int[] mul)
{
var count = 0;
foreach (var next in tree[cur])
{
if (next.to == prev) continue;
var sub = DFS(next.to, cur, tree, mul);
mul[next.id] = sub;
count += sub;
}
if (count == 0) ++count;
return count;
}
}