結果

問題 No.1103 Directed Length Sum
ユーザー tonyu0tonyu0
提出日時 2020-07-03 23:24:29
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 975 bytes
コンパイル時間 877 ms
コンパイル使用メモリ 174,936 KB
実行使用メモリ 93,416 KB
最終ジャッジ日時 2023-10-17 06:22:10
合計ジャッジ時間 6,478 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 0 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 80 ms
57,588 KB
testcase_04 AC 270 ms
41,740 KB
testcase_05 AC 530 ms
70,628 KB
testcase_06 AC 150 ms
27,496 KB
testcase_07 AC 21 ms
8,068 KB
testcase_08 AC 35 ms
11,152 KB
testcase_09 AC 13 ms
5,692 KB
testcase_10 AC 55 ms
14,744 KB
testcase_11 AC 301 ms
45,232 KB
testcase_12 AC 162 ms
27,828 KB
testcase_13 AC 64 ms
15,120 KB
testcase_14 AC 10 ms
4,780 KB
testcase_15 AC 114 ms
22,156 KB
testcase_16 AC 356 ms
50,492 KB
testcase_17 AC 371 ms
54,432 KB
testcase_18 AC 53 ms
14,676 KB
testcase_19 AC 309 ms
46,292 KB
testcase_20 AC 15 ms
6,520 KB
testcase_21 AC 32 ms
10,480 KB
testcase_22 AC 237 ms
38,324 KB
testcase_23 AC 115 ms
23,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

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];
    let mut deg = vec![0; n];
    for _ in 0..n - 1 {
        let a = itr.next().unwrap().parse::<usize>().unwrap() - 1;
        let b = itr.next().unwrap().parse::<usize>().unwrap() - 1;
        g[a].push(b);
        deg[b] += 1;
    }

    let mut start = 0;
    for i in 0..n {
        if deg[i] == 0 {
            start = i;
        }
    }

    let mut q = std::collections::VecDeque::new();
    let mut dist = vec![0; n];
    q.push_back(start);

    let mut ans = 0;
    while let Some(v) = q.pop_front() {
        ans += dist[v] * (dist[v] + 1) / 2;
        for &nv in g[v].iter() {
            dist[nv] = dist[v] + 1;
            q.push_back(nv);
        }
    }
    println!("{}", ans);
}
0