結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー phsplsphspls
提出日時 2022-11-09 02:26:53
言語 Rust
(1.77.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 1,023 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 144,328 KB
実行使用メモリ 21,960 KB
最終ジャッジ日時 2023-09-29 15:31:16
合計ジャッジ時間 5,632 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 60 ms
12,964 KB
testcase_04 AC 58 ms
13,008 KB
testcase_05 AC 64 ms
12,964 KB
testcase_06 AC 60 ms
13,004 KB
testcase_07 AC 54 ms
12,944 KB
testcase_08 AC 32 ms
9,268 KB
testcase_09 AC 15 ms
5,308 KB
testcase_10 AC 16 ms
5,576 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 27 ms
7,948 KB
testcase_13 AC 31 ms
9,024 KB
testcase_14 AC 31 ms
9,312 KB
testcase_15 AC 22 ms
7,404 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 47 ms
12,176 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 23 ms
7,156 KB
testcase_22 AC 19 ms
6,344 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 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 2 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 12 ms
6,392 KB
testcase_34 AC 60 ms
21,960 KB
testcase_35 AC 23 ms
10,368 KB
testcase_36 AC 5 ms
4,380 KB
testcase_37 AC 38 ms
13,504 KB
testcase_38 AC 34 ms
12,784 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<(usize, usize)>>, costs: &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, costs);
        ret += val;
        costs[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 temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let a = temp[0]-1;
        let b = temp[1]-1;
        paths[a].push((b, i));
        paths[b].push((a, i));
    }

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