結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー phsplsphspls
提出日時 2022-11-23 00:59:29
言語 Rust
(1.77.0)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,009 bytes
コンパイル時間 4,114 ms
コンパイル使用メモリ 164,816 KB
実行使用メモリ 21,932 KB
最終ジャッジ日時 2023-10-24 15:19:48
合計ジャッジ時間 5,152 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 60 ms
12,956 KB
testcase_04 AC 62 ms
12,956 KB
testcase_05 AC 62 ms
12,956 KB
testcase_06 AC 64 ms
12,956 KB
testcase_07 AC 62 ms
12,956 KB
testcase_08 AC 37 ms
9,260 KB
testcase_09 AC 15 ms
5,300 KB
testcase_10 AC 17 ms
5,564 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 28 ms
7,940 KB
testcase_13 AC 33 ms
8,996 KB
testcase_14 AC 33 ms
9,260 KB
testcase_15 AC 23 ms
7,412 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 54 ms
12,164 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 23 ms
7,148 KB
testcase_22 AC 19 ms
6,356 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 12 ms
6,356 KB
testcase_34 AC 67 ms
21,932 KB
testcase_35 AC 25 ms
10,316 KB
testcase_36 AC 5 ms
4,348 KB
testcase_37 AC 41 ms
13,472 KB
testcase_38 AC 37 ms
12,772 KB
testcase_39 AC 0 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 0 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<(usize, usize)>>, lines: &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, lines) ;
        lines[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 lines = vec![0usize; n-1];
    dfs(0, 0, &paths, &mut lines);
    let mut result = n * n;
    for &v in lines.iter() {
        result += (n - v) * v * 2;
    }
    println!("{}", result);
}
0