結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー phsplsphspls
提出日時 2022-11-16 00:34:12
言語 Rust
(1.77.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,002 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 175,776 KB
実行使用メモリ 21,936 KB
最終ジャッジ日時 2023-10-16 23:13:24
合計ジャッジ時間 5,710 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 0 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 61 ms
12,960 KB
testcase_04 AC 59 ms
12,960 KB
testcase_05 AC 61 ms
12,960 KB
testcase_06 AC 60 ms
12,960 KB
testcase_07 AC 61 ms
12,960 KB
testcase_08 AC 36 ms
9,264 KB
testcase_09 AC 15 ms
5,304 KB
testcase_10 AC 16 ms
5,568 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 28 ms
7,944 KB
testcase_13 AC 34 ms
9,000 KB
testcase_14 AC 36 ms
9,264 KB
testcase_15 AC 26 ms
7,416 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 58 ms
12,168 KB
testcase_19 AC 8 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 24 ms
7,152 KB
testcase_22 AC 20 ms
6,360 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 3 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 2 ms
4,348 KB
testcase_30 AC 2 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
6,360 KB
testcase_34 AC 67 ms
21,936 KB
testcase_35 AC 24 ms
10,320 KB
testcase_36 AC 5 ms
4,348 KB
testcase_37 AC 42 ms
13,476 KB
testcase_38 AC 36 ms
12,776 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 #

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);
        cnts[idx] = val;
        ret += 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 += v * (n - v) * 2;
    }
    println!("{}", result);
}
0