結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー koba-e964koba-e964
提出日時 2021-10-15 09:42:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 1,977 bytes
コンパイル時間 1,492 ms
コンパイル使用メモリ 186,532 KB
実行使用メモリ 18,460 KB
最終ジャッジ日時 2023-10-17 19:34:47
合計ジャッジ時間 5,553 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 60 ms
9,740 KB
testcase_04 AC 62 ms
9,740 KB
testcase_05 AC 59 ms
9,740 KB
testcase_06 AC 58 ms
9,740 KB
testcase_07 AC 57 ms
9,740 KB
testcase_08 AC 37 ms
6,836 KB
testcase_09 AC 16 ms
4,348 KB
testcase_10 AC 16 ms
4,348 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 26 ms
5,516 KB
testcase_13 AC 33 ms
6,308 KB
testcase_14 AC 35 ms
6,572 KB
testcase_15 AC 24 ms
5,252 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 48 ms
8,948 KB
testcase_19 AC 8 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 23 ms
4,988 KB
testcase_22 AC 22 ms
4,460 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 11 ms
5,716 KB
testcase_34 AC 61 ms
18,460 KB
testcase_35 AC 23 ms
9,348 KB
testcase_36 AC 5 ms
4,348 KB
testcase_37 AC 42 ms
9,628 KB
testcase_38 AC 39 ms
9,076 KB
testcase_39 AC 1 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

fn dfs(v: usize, par: usize, g: &[Vec<usize>]) -> (i64, i64) {
    let n = g.len();
    let mut sz = 1;
    let mut tot = 0;
    for &w in &g[v] {
        if w == par { continue; }
        let (subsz, subtot) = dfs(w, v, g);
        tot += subsz * (n as i64 - subsz);
        sz += subsz;
        tot += subtot;
    }
    (sz, tot)
}

// Tags: sum-of-distances-on-trees
fn solve() {
    input! {
        n: usize,
        ab: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for &(a, b) in &ab {
        g[a].push(b);
        g[b].push(a);
    }
    let (_sz, tot) = dfs(0, n, &g);
    println!("{}", 2 * tot + n as i64 * n as i64);
}
0