結果

問題 No.1221 木 *= 3
ユーザー phsplsphspls
提出日時 2022-11-09 11:11:21
言語 Rust
(1.77.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 3,196 ms
コンパイル使用メモリ 137,588 KB
実行使用メモリ 29,828 KB
最終ジャッジ日時 2023-09-29 23:24:14
合計ジャッジ時間 6,534 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,504 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 63 ms
29,828 KB
testcase_08 AC 63 ms
29,308 KB
testcase_09 AC 64 ms
29,712 KB
testcase_10 AC 70 ms
29,436 KB
testcase_11 AC 67 ms
29,412 KB
testcase_12 AC 58 ms
18,828 KB
testcase_13 AC 54 ms
18,524 KB
testcase_14 AC 57 ms
18,684 KB
testcase_15 AC 58 ms
18,628 KB
testcase_16 AC 58 ms
18,604 KB
testcase_17 AC 62 ms
18,112 KB
testcase_18 AC 63 ms
18,060 KB
testcase_19 AC 63 ms
18,028 KB
testcase_20 AC 63 ms
18,180 KB
testcase_21 AC 62 ms
18,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const INF: isize = 1isize << 60;

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<usize>>, a: &Vec<isize>, b: &Vec<isize>, dp: &mut Vec<Vec<isize>>) {
    let mut score = 0isize;
    let mut del_score = 0isize;
    for &v in paths[cur].iter() {
        if prev == v { continue; }
        dfs(v, cur, paths, a, b, dp);
        let used_score = b[v] + b[cur] + dp[v][1];
        let unused_score = dp[v][0];
        score += used_score.max(unused_score);
        del_score += dp[v][0].max(dp[v][1]);
    }
    dp[cur][0] = a[cur] + del_score;
    dp[cur][1] = score;
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<isize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut b = String::new();
    std::io::stdin().read_line(&mut b).ok();
    let b: Vec<isize> = b.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let mut paths = vec![vec![]; n];
    for _ in 0..n-1 {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        paths[a].push(b);
        paths[b].push(a);
    }

    let mut dp = vec![vec![-INF; 2]; n];
    dfs(0, 0, &paths, &a, &b, &mut dp);
    println!("{}", dp[0][0].max(dp[0][1]));
}
0