結果
問題 | No.1637 Easy Tree Query |
ユーザー |
![]() |
提出日時 | 2021-08-06 21:32:57 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 182 ms / 2,000 ms |
コード長 | 1,560 bytes |
コンパイル時間 | 14,392 ms |
コンパイル使用メモリ | 377,520 KB |
実行使用メモリ | 15,744 KB |
最終ジャッジ日時 | 2024-09-17 03:44:18 |
合計ジャッジ時間 | 16,711 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
コンパイルメッセージ
warning: unused variable: `i` --> src/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` --> src/main.rs:29:9 | 29 | for i in 0..q { | ^ help: if this is intentional, prefix it with an underscore: `_i`
ソースコード
#![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() }