結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tonyu0tonyu0
提出日時 2021-03-05 23:30:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 1,362 ms
コンパイル使用メモリ 155,008 KB
実行使用メモリ 20,736 KB
最終ジャッジ日時 2024-04-16 11:36:14
合計ジャッジ時間 2,692 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 0 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 53 ms
11,648 KB
testcase_04 AC 52 ms
11,776 KB
testcase_05 AC 55 ms
11,776 KB
testcase_06 AC 54 ms
11,776 KB
testcase_07 AC 50 ms
11,776 KB
testcase_08 AC 28 ms
8,448 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 20 ms
7,168 KB
testcase_13 AC 24 ms
7,936 KB
testcase_14 AC 25 ms
8,320 KB
testcase_15 AC 20 ms
6,656 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 49 ms
10,880 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 18 ms
6,528 KB
testcase_22 AC 15 ms
5,888 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 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 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 9 ms
6,272 KB
testcase_34 AC 55 ms
20,736 KB
testcase_35 AC 19 ms
9,984 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 30 ms
11,392 KB
testcase_38 AC 28 ms
10,624 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

// 0を根とした各部分木のサイズ
fn dfs1(v: usize, p: usize, sz: &mut Vec<usize>, g: &Vec<Vec<usize>>) -> usize {
    for &nv in g[v].iter() {
        if nv != p {
            sz[v] += dfs1(nv, v, sz, &g);
        }
    }
    sz[v]
}
// vを根とした状態になってる
fn dfs2(v: usize, p: usize, sz: &mut Vec<usize>, ans: &mut Vec<usize>, g: &Vec<Vec<usize>>) {
    for &nv in g[v].iter() {
        ans[v] += (g.len() - sz[nv]) * sz[nv];
    }
    for &nv in g[v].iter() {
        if nv != p {
            sz[v] -= sz[nv];
            sz[nv] += sz[v];
            dfs2(nv, v, sz, ans, &g);
            sz[nv] -= sz[v];
            sz[v] += sz[nv];
        }
    }
}

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 = vec![0; n];
    let mut size = vec![1; n];
    dfs1(0, 0, &mut size, &g);
    dfs2(0, 0, &mut size, &mut ans, &g);
    let s: usize = ans.iter().sum();
    println!("{}", s + n * n);
}
0