結果

問題 No.1637 Easy Tree Query
ユーザー phsplsphspls
提出日時 2022-10-23 20:02:14
言語 Rust
(1.77.0)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,317 bytes
コンパイル時間 1,477 ms
コンパイル使用メモリ 146,804 KB
実行使用メモリ 19,096 KB
最終ジャッジ日時 2023-09-15 05:30:09
合計ジャッジ時間 9,045 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 170 ms
11,152 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 38 ms
6,152 KB
testcase_05 AC 154 ms
6,412 KB
testcase_06 AC 100 ms
5,100 KB
testcase_07 AC 56 ms
4,384 KB
testcase_08 AC 29 ms
5,372 KB
testcase_09 AC 114 ms
8,760 KB
testcase_10 AC 68 ms
4,380 KB
testcase_11 AC 158 ms
9,820 KB
testcase_12 AC 159 ms
8,540 KB
testcase_13 AC 23 ms
4,384 KB
testcase_14 AC 68 ms
8,272 KB
testcase_15 AC 148 ms
9,864 KB
testcase_16 AC 110 ms
10,100 KB
testcase_17 AC 44 ms
4,384 KB
testcase_18 AC 143 ms
6,120 KB
testcase_19 AC 138 ms
6,672 KB
testcase_20 AC 171 ms
8,264 KB
testcase_21 AC 81 ms
6,608 KB
testcase_22 AC 175 ms
10,904 KB
testcase_23 AC 40 ms
4,516 KB
testcase_24 AC 62 ms
6,384 KB
testcase_25 AC 156 ms
6,080 KB
testcase_26 AC 176 ms
8,784 KB
testcase_27 AC 197 ms
10,920 KB
testcase_28 AC 110 ms
5,352 KB
testcase_29 AC 131 ms
7,704 KB
testcase_30 AC 36 ms
6,648 KB
testcase_31 AC 130 ms
4,384 KB
testcase_32 AC 67 ms
5,360 KB
testcase_33 AC 148 ms
5,092 KB
testcase_34 AC 37 ms
19,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<usize>>, dp: &mut Vec<usize>) {
    let mut ret = 1usize;
    for &v in paths[cur].iter() {
        if v == prev { continue; }
        dfs(v, cur, paths, dp);
        ret += dp[v];
    }
    dp[cur] = ret;
}

fn main() {
    let mut nq = String::new();
    std::io::stdin().read_line(&mut nq).ok();
    let nq: Vec<usize> = nq.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nq[0];
    let q = nq[1];
    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 queries = (0..q).map(|_| {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        (temp[0]-1, temp[1])
    })
    .collect::<Vec<_>>();

    let mut dp = vec![0usize; n];
    dfs(0, 0, &paths, &mut dp);
    let mut result = 0usize;
    for &(p, x) in queries.iter() {
        result += dp[p] * x;
        println!("{}", result);
    }
}
0