結果

問題 No.827 総神童数
ユーザー phsplsphspls
提出日時 2023-01-05 01:46:37
言語 Rust
(1.77.0)
結果
AC  
実行時間 156 ms / 2,000 ms
コード長 1,314 bytes
コンパイル時間 7,012 ms
コンパイル使用メモリ 139,532 KB
実行使用メモリ 31,644 KB
最終ジャッジ日時 2023-08-18 23:46:59
合計ジャッジ時間 12,905 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 0 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 0 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 156 ms
31,644 KB
testcase_10 AC 48 ms
11,136 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 21 ms
6,424 KB
testcase_13 AC 120 ms
26,368 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 44 ms
11,648 KB
testcase_16 AC 118 ms
21,084 KB
testcase_17 AC 144 ms
25,624 KB
testcase_18 AC 59 ms
13,800 KB
testcase_19 AC 155 ms
17,100 KB
testcase_20 AC 109 ms
13,224 KB
testcase_21 AC 76 ms
10,040 KB
testcase_22 AC 136 ms
14,264 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 137 ms
15,576 KB
testcase_25 AC 98 ms
12,168 KB
testcase_26 AC 136 ms
15,572 KB
testcase_27 AC 87 ms
11,052 KB
testcase_28 AC 85 ms
10,828 KB
testcase_29 AC 63 ms
9,048 KB
testcase_30 AC 19 ms
4,380 KB
testcase_31 AC 51 ms
7,420 KB
testcase_32 AC 48 ms
7,180 KB
testcase_33 AC 143 ms
16,852 KB
testcase_34 AC 136 ms
15,536 KB
testcase_35 AC 49 ms
7,736 KB
testcase_36 AC 73 ms
9,764 KB
testcase_37 AC 99 ms
12,136 KB
testcase_38 AC 148 ms
16,576 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 v == prev { 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];
    depths[0] = 0;
    dfs(0, 0, &paths, &mut depths);
    let total_pattern = (1..=n).fold(1usize, |x, y| x * y % MOD);
    let mut result = 0usize;
    for i in 0..n {
        result += total_pattern * power(depths[i]+1, MOD-2) % MOD;
        result %= MOD;
    }
    println!("{}", result);
}
0