結果

問題 No.1488 Max Score of the Tree
ユーザー StrorkisStrorkis
提出日時 2021-04-23 22:49:03
言語 Rust
(1.77.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,461 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 153,268 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-17 12:50:17
合計ジャッジ時間 2,157 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

fn dfs(
    g: &[Vec<(usize, usize)>], cnt: &mut [usize],
    prev: usize, from: usize,
) -> usize {
    let mut res = 0;
    for &(to, i) in &g[from] {
        if to == prev { continue; }
        let x = dfs(g, cnt, from, to);
        cnt[i] += x;
        res += x;
    }
    res.max(1)
}

fn main() {
    let ref mut buf = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), buf).ok();
    let mut iter = buf.split_whitespace();

    macro_rules! scan {
        ([$t:tt; $n:expr]) => ((0..$n).map(|_| scan!($t)).collect::<Vec<_>>());
        (($($t:tt),*)) => (($(scan!($t)),*));
        (Usize1) => (scan!(usize) - 1);
        (Bytes) => (scan!(String).into_bytes());
        ($t:ty) => (iter.next().unwrap().parse::<$t>().unwrap());
    }

    let (n, k) = scan!((usize, usize));

    let mut g = vec![vec![]; n];
    let mut l = vec![0; n - 1];
    for i in 0..(n - 1) {
        let (a, b, c) = scan!((Usize1, Usize1, usize));
        g[a].push((b, i));
        g[b].push((a, i));
        l[i] = c;
    }

    let mut cnt = vec![0; n - 1];
    dfs(&g, &mut cnt, !0, 0);

    let mut ans = 0;
    let mut dp = vec![0; k + 1];
    dp[0] = 0;
    for (l, cnt) in l.into_iter().zip(cnt.into_iter()) {
        ans += l * cnt;
        for i in (0..=k).rev() {
            if i + l <= k {
                dp[i + l] = dp[i + l].max(dp[i] + l * cnt);
            }
        }
    }

    ans += dp.iter().max().unwrap();
    println!("{}", ans);
}
0