結果

問題 No.1637 Easy Tree Query
ユーザー fukafukatanifukafukatani
提出日時 2021-08-06 21:32:57
言語 Rust
(1.77.0)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 1,560 bytes
コンパイル時間 4,666 ms
コンパイル使用メモリ 172,080 KB
実行使用メモリ 15,864 KB
最終ジャッジ日時 2023-10-17 05:01:32
合計ジャッジ時間 8,166 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 176 ms
11,464 KB
testcase_03 AC 0 ms
4,348 KB
testcase_04 AC 39 ms
6,148 KB
testcase_05 AC 159 ms
6,344 KB
testcase_06 AC 102 ms
5,040 KB
testcase_07 AC 57 ms
4,348 KB
testcase_08 AC 31 ms
5,304 KB
testcase_09 AC 115 ms
8,784 KB
testcase_10 AC 70 ms
4,380 KB
testcase_11 AC 165 ms
9,880 KB
testcase_12 AC 165 ms
8,500 KB
testcase_13 AC 23 ms
4,348 KB
testcase_14 AC 66 ms
8,452 KB
testcase_15 AC 149 ms
9,884 KB
testcase_16 AC 110 ms
10,388 KB
testcase_17 AC 44 ms
4,348 KB
testcase_18 AC 146 ms
6,248 KB
testcase_19 AC 142 ms
6,856 KB
testcase_20 AC 177 ms
8,464 KB
testcase_21 AC 82 ms
6,688 KB
testcase_22 AC 179 ms
11,096 KB
testcase_23 AC 42 ms
4,656 KB
testcase_24 AC 64 ms
6,708 KB
testcase_25 AC 162 ms
6,100 KB
testcase_26 AC 178 ms
8,764 KB
testcase_27 AC 200 ms
10,936 KB
testcase_28 AC 117 ms
5,420 KB
testcase_29 AC 136 ms
7,616 KB
testcase_30 AC 35 ms
6,744 KB
testcase_31 AC 135 ms
4,348 KB
testcase_32 AC 68 ms
5,348 KB
testcase_33 AC 154 ms
5,196 KB
testcase_34 AC 36 ms
15,864 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:22:9
   |
22 |     for i in 0..n - 1 {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `i`
  --> Main.rs:29:9
   |
29 |     for i in 0..q {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: 2 warnings emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, q) = (v[0], v[1]);
    let mut edges = vec![vec![]; n];
    for i in 0..n - 1 {
        let v = read_vec::<usize>();
        let (a, b) = (v[0] - 1, v[1] - 1);
        edges[a].push(b);
        edges[b].push(a);
    }
    let mut queries = vec![];
    for i in 0..q {
        let v = read_vec::<i64>();
        let (a, b) = (v[0] as usize - 1, v[1]);
        queries.push((a, b));
    }
    let mut child = vec![0i64; n];
    dfs(0, 0, &edges, &mut child);
    let mut ans = 0;
    for (p, x) in queries {
        ans += child[p] * x;
        println!("{}", ans);
    }
}

fn dfs(cur: usize, parent: usize, edges: &Vec<Vec<usize>>, child: &mut Vec<i64>) -> i64 {
    child[cur] = 1;
    for &to in edges[cur].iter().filter(|&&x| x != parent) {
        child[cur] += dfs(to, cur, edges, child);
    }
    child[cur]
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0