結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tonyu0tonyu0
提出日時 2021-03-05 23:46:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 895 bytes
コンパイル時間 743 ms
コンパイル使用メモリ 164,268 KB
実行使用メモリ 19,200 KB
最終ジャッジ日時 2024-04-16 11:51:59
合計ジャッジ時間 2,707 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 33 ms
10,112 KB
testcase_04 AC 33 ms
10,240 KB
testcase_05 AC 36 ms
10,112 KB
testcase_06 AC 34 ms
10,112 KB
testcase_07 AC 35 ms
10,112 KB
testcase_08 AC 22 ms
7,296 KB
testcase_09 AC 10 ms
5,376 KB
testcase_10 AC 10 ms
5,376 KB
testcase_11 AC 6 ms
5,376 KB
testcase_12 AC 15 ms
6,400 KB
testcase_13 AC 16 ms
7,040 KB
testcase_14 AC 19 ms
7,296 KB
testcase_15 AC 13 ms
5,888 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 28 ms
9,472 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 15 ms
6,016 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 7 ms
5,760 KB
testcase_34 AC 45 ms
19,200 KB
testcase_35 AC 14 ms
9,088 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 23 ms
9,856 KB
testcase_38 AC 22 ms
9,344 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 0 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn dfs(v: usize, p: usize, ans: &mut usize, g: &Vec<Vec<usize>>) -> usize {
    let mut sz = 1;
    for &nv in g[v].iter() {
        if nv != p {
            let nsz = dfs(nv, v, ans, g);
            *ans += nsz * (g.len() - nsz);
            sz += nsz;
        }
    }
    *ans += sz * (g.len() - sz);
    sz
}
fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let mut g: Vec<Vec<usize>> = vec![Vec::new(); n];
    for _ in 0..n - 1 {
        let a: usize = itr.next().unwrap().parse::<usize>().unwrap() - 1;
        let b: usize = itr.next().unwrap().parse::<usize>().unwrap() - 1;
        g[a].push(b);
        g[b].push(a);
    }
    let mut ans = 0;
    dfs(0, 0, &mut ans, &g);
    println!("{}", ans + n * n);
}
0