結果

問題 No.1103 Directed Length Sum
ユーザー tonyu0tonyu0
提出日時 2020-07-03 23:25:19
言語 Rust
(1.72.1)
結果
WA  
実行時間 -
コード長 981 bytes
コンパイル時間 4,527 ms
コンパイル使用メモリ 166,308 KB
実行使用メモリ 97,232 KB
最終ジャッジ日時 2023-10-17 06:22:25
合計ジャッジ時間 9,041 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 86 ms
61,496 KB
testcase_04 AC 301 ms
43,944 KB
testcase_05 AC 573 ms
74,436 KB
testcase_06 AC 174 ms
28,928 KB
testcase_07 AC 24 ms
8,336 KB
testcase_08 AC 42 ms
11,684 KB
testcase_09 AC 14 ms
5,960 KB
testcase_10 AC 70 ms
15,468 KB
testcase_11 AC 341 ms
47,632 KB
testcase_12 AC 180 ms
29,276 KB
testcase_13 AC 72 ms
15,864 KB
testcase_14 AC 10 ms
4,948 KB
testcase_15 AC 130 ms
23,284 KB
testcase_16 AC 392 ms
53,192 KB
testcase_17 AC 420 ms
57,248 KB
testcase_18 AC 67 ms
15,396 KB
testcase_19 AC 351 ms
48,752 KB
testcase_20 AC 17 ms
6,788 KB
testcase_21 AC 39 ms
10,960 KB
testcase_22 AC 276 ms
40,336 KB
testcase_23 AC 143 ms
24,684 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![0u64; n];
    q.push_back(start);

    let mut ans = 0u64;
    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