結果

問題 No.417 チューリップバブル
ユーザー ngtkana
提出日時 2024-06-10 20:29:53
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 3,044 bytes
コンパイル時間 17,287 ms
コンパイル使用メモリ 388,512 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-01-02 17:46:40
合計ジャッジ時間 19,180 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;
use std::collections::HashMap;

fn main() {
    input! {
        n: usize,
        m: usize,
        values: [u64; n],
        edges: [(usize, usize, usize); n - 1],
    }
    let mut g = vec![Vec::new(); n];
    let mut weight = HashMap::new();
    for &(i, j, w) in &edges {
        g[i].push(j);
        g[j].push(i);
        weight.insert((i, j), w);
        weight.insert((j, i), w);
    }
    let mut sorted = Vec::new();
    let mut parent = vec![usize::MAX; n];
    let mut stack = vec![0];
    while let Some(i) = stack.pop() {
        sorted.push(i);
        g[i].retain(|&j| j != parent[i]);
        for &j in &g[i] {
            stack.push(j);
            parent[j] = i;
        }
    }
    let weights = (0..n)
        .map(|i| if i == 0 { 0 } else { weight[&(parent[i], i)] })
        .collect::<Vec<_>>();

    let dp = tree_dp(
        &O {
            max: m / 2,
            weights: &weights,
            values: &values,
        },
        &g,
        &sorted,
    );
    let ans = dp[0].f.iter().copied().max().unwrap().unwrap();
    println!("{ans}");
}

#[derive(Debug, Clone, PartialEq, Default)]
struct Value {
    f: Vec<Option<u64>>,
}

struct O<'a> {
    max: usize,
    weights: &'a [usize],
    values: &'a [u64],
}
impl Op for O<'_> {
    type Acc = Value;
    type Value = Value;

    fn f(&self, value: &Self::Value, index: usize) -> Self::Acc {
        let w = self.weights[index].min(self.max + 1);
        let mut f = value.f.clone();
        f.rotate_right(w);
        f[..w].iter_mut().for_each(|x| *x = None);
        f[0] = Some(0);
        Self::Acc { f }
    }

    fn g(&self, acc: &Self::Acc, index: usize) -> Self::Value {
        let v = self.values[index];
        let mut f = acc.f.clone();
        f.iter_mut().for_each(|x| *x = x.map(|x| x + v));
        Self::Value { f }
    }

    fn identity(&self) -> Self::Acc {
        let mut f = vec![None; self.max + 1];
        f[0] = Some(0);
        Self::Acc { f }
    }

    fn mul(&self, lhs: &Self::Acc, rhs: &Self::Acc) -> Self::Acc {
        let mut f = vec![None; self.max + 1];
        f[0] = Some(0);
        for (i, &x) in lhs.f.iter().enumerate() {
            for (j, &y) in rhs.f[..=self.max - i].iter().enumerate() {
                f[i + j] = f[i + j].max(x.zip(y).map(|(x, y)| x + y));
            }
        }
        Self::Acc { f }
    }
}

pub trait Op {
    type Value: Default + Clone;
    type Acc: Default + Clone;

    fn f(&self, value: &Self::Value, index: usize) -> Self::Acc;
    fn g(&self, acc: &Self::Acc, index: usize) -> Self::Value;
    fn identity(&self) -> Self::Acc;
    fn mul(&self, lhs: &Self::Acc, rhs: &Self::Acc) -> Self::Acc;
}

pub fn tree_dp<O: Op>(o: &O, g: &[Vec<usize>], sorted: &[usize]) -> Vec<O::Value> {
    let mut dp = vec![O::Value::default(); g.len()];
    for &i in sorted.iter().rev() {
        dp[i] = o.g(
            &g[i]
                .iter()
                .fold(o.identity(), |acc, &j| o.mul(&acc, &o.f(&dp[j], j))),
            i,
        );
    }
    dp
}
0