結果

問題 No.1507 Road Blocked
ユーザー StrorkisStrorkis
提出日時 2021-05-15 11:36:32
言語 Rust
(1.77.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 3,478 bytes
コンパイル時間 1,189 ms
コンパイル使用メモリ 165,072 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-10 15:55:47
合計ジャッジ時間 3,323 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 23 ms
19,456 KB
testcase_04 AC 24 ms
10,112 KB
testcase_05 AC 24 ms
10,112 KB
testcase_06 AC 23 ms
10,112 KB
testcase_07 AC 24 ms
10,112 KB
testcase_08 AC 24 ms
9,984 KB
testcase_09 AC 23 ms
10,112 KB
testcase_10 AC 24 ms
10,112 KB
testcase_11 AC 24 ms
10,112 KB
testcase_12 AC 23 ms
10,112 KB
testcase_13 AC 23 ms
10,236 KB
testcase_14 AC 23 ms
10,096 KB
testcase_15 AC 24 ms
10,112 KB
testcase_16 AC 24 ms
10,112 KB
testcase_17 AC 23 ms
9,984 KB
testcase_18 AC 25 ms
10,112 KB
testcase_19 AC 24 ms
10,104 KB
testcase_20 AC 25 ms
10,112 KB
testcase_21 AC 25 ms
9,984 KB
testcase_22 AC 24 ms
9,984 KB
testcase_23 AC 26 ms
10,020 KB
testcase_24 AC 24 ms
10,240 KB
testcase_25 AC 24 ms
10,112 KB
testcase_26 AC 25 ms
10,112 KB
testcase_27 AC 25 ms
10,112 KB
testcase_28 AC 25 ms
10,112 KB
testcase_29 AC 24 ms
10,112 KB
testcase_30 AC 25 ms
10,112 KB
testcase_31 AC 25 ms
10,204 KB
testcase_32 AC 25 ms
10,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod mod_int {
    use std::{fmt, ops};

    #[derive(Clone, Copy, Debug)]
    pub struct ModInt(u64);

    impl ModInt {
        const MOD: u64 = 998_244_353;

        pub fn new(mut x: u64) -> Self {
            if x >= Self::MOD {
                x %= Self::MOD;
            }
            Self(x)
        }

        pub fn zero() -> Self {
            Self(0)
        }

        pub fn one() -> Self {
            Self(1)
        }
    }

    impl From<usize> for ModInt {
        fn from(x: usize) -> Self {
            Self::new(x as u64)
        }
    }

    impl From<i64> for ModInt {
        fn from(mut x: i64) -> Self {
            let m = Self::MOD as i64;
            if x.abs() >= m {
                x %= m;
            }
            if x < 0 {
                x += m
            }
            Self::new(x as u64)
        }
    }

    impl ops::Add for ModInt {
        type Output = Self;

        fn add(self, other: Self) -> Self {
            let mut x = self.0 + other.0;
            if x >= Self::MOD {
                x -= Self::MOD;
            }
            Self(x)
        }
    }

    impl ops::AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }

    impl ops::Sub for ModInt {
        type Output = Self;

        fn sub(mut self, other: Self) -> Self {
            if self.0 < other.0 {
                self.0 += Self::MOD;
            }
            Self(self.0 - other.0)
        }
    }

    impl ops::SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }

    impl ops::Mul for ModInt {
        type Output = Self;

        fn mul(self, other: Self) -> Self {
            Self::new(self.0 * other.0)
        }
    }

    impl ops::MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }

    impl fmt::Display for ModInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl ModInt {
        pub fn pow(mut self, mut n: u64) -> Self {
            let mut res = Self::one();
            while n > 0 {
                if n & 1 == 1 {
                    res *= self;
                }
                self *= self;
                n >>= 1;
            }
            res
        }
        
        pub fn inv(self) -> Self {
            self.pow(Self::MOD - 2)
        }
    }
}

use mod_int::ModInt;

fn dfs(
    ans: &mut ModInt,
    n: usize, g: &[Vec<usize>], p: ModInt,
    prev: usize, from: usize
) -> usize {
    let mut count = 0;
    for &to in &g[from] {
        if to == prev { continue; }
        let c = dfs(ans, n, g, p, from, to);
        *ans = *ans - ModInt::from(c * (n - c)) * p;
        count += c;
    }
    count + 1
}

fn main() {
    let ref mut buf = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), buf).ok();
    let mut iter = buf.split_whitespace();

    macro_rules! scan {
        ($t:ty) => (iter.next().unwrap().parse::<$t>().unwrap());
    }

    let n = scan!(usize);
    let mut g = vec![vec![]; n];
    for _ in 0..(n - 1) {
        let (u, v) = (scan!(usize) - 1, scan!(usize) - 1);
        g[u].push(v);
        g[v].push(u);
    }

    let mut ans = ModInt::one();
    let k = n * (n - 1) / 2;
    let p = ModInt::from((n - 1) * k).inv();
    dfs(&mut ans, n, &g, p, !0, 0);
    println!("{}", ans);
}
0