結果

問題 No.827 総神童数
ユーザー phsplsphspls
提出日時 2022-12-22 23:41:30
言語 Rust
(1.77.0)
結果
AC  
実行時間 213 ms / 2,000 ms
コード長 1,296 bytes
コンパイル時間 13,227 ms
コンパイル使用メモリ 379,792 KB
実行使用メモリ 33,152 KB
最終ジャッジ日時 2024-04-29 04:01:51
合計ジャッジ時間 18,338 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 0 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 196 ms
33,152 KB
testcase_10 AC 60 ms
11,776 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 25 ms
6,784 KB
testcase_13 AC 148 ms
27,520 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 57 ms
12,032 KB
testcase_16 AC 151 ms
22,400 KB
testcase_17 AC 180 ms
27,136 KB
testcase_18 AC 73 ms
14,464 KB
testcase_19 AC 213 ms
18,688 KB
testcase_20 AC 150 ms
14,208 KB
testcase_21 AC 102 ms
10,880 KB
testcase_22 AC 169 ms
15,360 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 190 ms
16,768 KB
testcase_25 AC 138 ms
13,312 KB
testcase_26 AC 190 ms
16,896 KB
testcase_27 AC 116 ms
11,904 KB
testcase_28 AC 116 ms
11,776 KB
testcase_29 AC 89 ms
9,856 KB
testcase_30 AC 24 ms
5,376 KB
testcase_31 AC 63 ms
8,192 KB
testcase_32 AC 65 ms
7,808 KB
testcase_33 AC 211 ms
18,176 KB
testcase_34 AC 190 ms
16,768 KB
testcase_35 AC 64 ms
8,192 KB
testcase_36 AC 107 ms
10,752 KB
testcase_37 AC 138 ms
13,312 KB
testcase_38 AC 207 ms
17,920 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const MOD: usize = 1e9 as usize + 7;

fn power(base: usize, times: usize) -> usize {
    if times == 0 { return 1usize; }
    if times == 1 { return base; }
    let temp = power(base, times/2);
    temp * temp % MOD * power(base, times%2) % MOD
}

fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<usize>>, depths: &mut Vec<usize>) {
    for &v in paths[cur].iter() {
        if prev == v { continue; }
        depths[v] = depths[cur] + 1;
        dfs(v, cur, paths, depths);
    }
}

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 _ 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 u = temp[0]-1;
        let v = temp[1]-1;
        paths[u].push(v);
        paths[v].push(u);
    }

    let mut depths = vec![0usize; n];
    dfs(0, 0, &paths, &mut depths);
    let total = (1..=n).fold(1usize, |x, y| x * y % MOD);
    let divs = (1..=n).map(|i| power(i, MOD-2)).collect::<Vec<_>>();
    let result = depths.iter().map(|&v| divs[v]).sum::<usize>() % MOD * total % MOD;
    println!("{}", result);
}
0