結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー phsplsphspls
提出日時 2022-11-10 00:06:23
言語 Rust
(1.77.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,000 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 143,268 KB
実行使用メモリ 21,964 KB
最終ジャッジ日時 2023-09-30 10:45:58
合計ジャッジ時間 4,065 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 64 ms
12,932 KB
testcase_04 AC 60 ms
13,004 KB
testcase_05 AC 58 ms
12,952 KB
testcase_06 AC 61 ms
12,980 KB
testcase_07 AC 56 ms
12,980 KB
testcase_08 AC 35 ms
9,284 KB
testcase_09 AC 16 ms
5,344 KB
testcase_10 AC 16 ms
5,556 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 26 ms
7,964 KB
testcase_13 AC 32 ms
9,044 KB
testcase_14 AC 35 ms
9,292 KB
testcase_15 AC 25 ms
7,440 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 60 ms
12,204 KB
testcase_19 AC 9 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 26 ms
7,128 KB
testcase_22 AC 22 ms
6,396 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,504 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 12 ms
6,404 KB
testcase_34 AC 70 ms
21,964 KB
testcase_35 AC 23 ms
10,348 KB
testcase_36 AC 5 ms
4,380 KB
testcase_37 AC 42 ms
13,424 KB
testcase_38 AC 39 ms
12,832 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<(usize, usize)>>, cnts: &mut Vec<usize>) -> usize {
    let mut ret = 1usize;
    for &(v, idx) in paths[cur].iter() {
        if v == prev { continue; }
        let val = dfs(v, cur, paths, cnts);
        ret += val;
        cnts[idx] = val;
    }
    ret
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut paths = vec![vec![]; n];
    for i in 0..n-1 {
        let mut ab = String::new();
        std::io::stdin().read_line(&mut ab).ok();
        let ab: Vec<usize> = ab.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = ab[0]-1;
        let b = ab[1]-1;
        paths[a].push((b, i));
        paths[b].push((a, i));
    }

    let mut cnts = vec![0usize; n-1];
    dfs(0, 0, &paths, &mut cnts);
    let mut result = n*n;
    for &v in cnts.iter() {
        result += (n - v) * v * 2;
    }
    println!("{}", result);
}
0