結果

問題 No.1507 Road Blocked
ユーザー Moss_LocalMoss_Local
提出日時 2021-05-14 22:45:42
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 16,086 bytes
コンパイル時間 16,020 ms
コンパイル使用メモリ 396,648 KB
実行使用メモリ 25,088 KB
最終ジャッジ日時 2024-10-02 03:06:38
合計ジャッジ時間 17,087 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 40 ms
25,088 KB
testcase_04 AC 71 ms
14,336 KB
testcase_05 AC 69 ms
14,208 KB
testcase_06 AC 72 ms
14,208 KB
testcase_07 AC 72 ms
14,208 KB
testcase_08 AC 71 ms
14,208 KB
testcase_09 AC 74 ms
14,208 KB
testcase_10 AC 70 ms
14,336 KB
testcase_11 AC 71 ms
14,208 KB
testcase_12 AC 71 ms
14,208 KB
testcase_13 AC 69 ms
14,208 KB
testcase_14 AC 68 ms
14,208 KB
testcase_15 AC 71 ms
14,336 KB
testcase_16 AC 70 ms
14,208 KB
testcase_17 AC 71 ms
14,208 KB
testcase_18 AC 72 ms
14,208 KB
testcase_19 AC 71 ms
14,208 KB
testcase_20 AC 72 ms
14,208 KB
testcase_21 AC 69 ms
14,208 KB
testcase_22 AC 69 ms
14,208 KB
testcase_23 AC 72 ms
14,208 KB
testcase_24 AC 75 ms
14,208 KB
testcase_25 AC 73 ms
14,180 KB
testcase_26 AC 109 ms
14,336 KB
testcase_27 AC 95 ms
14,208 KB
testcase_28 AC 73 ms
14,208 KB
testcase_29 AC 86 ms
14,336 KB
testcase_30 AC 74 ms
14,124 KB
testcase_31 AC 76 ms
14,120 KB
testcase_32 AC 71 ms
14,336 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unnecessary parentheses around type
  --> src/main.rs:70:15
   |
70 | fn readi() -> (i64) {
   |               ^   ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
70 - fn readi() -> (i64) {
70 + fn readi() -> i64 {
   |

warning: unused variable: `a2`
   --> src/main.rs:454:14
    |
454 |         let (a2, e2) = mod_fact(k, p, fact);
    |              ^^ help: if this is intentional, prefix it with an underscore: `_a2`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `a2`
   --> src/main.rs:502:18
    |
502 |             let (a2, e2) = mod_fact(k, p, &self.f_table);
    |                  ^^ help: if this is intentional, prefix it with an underscore: `_a2`

warning: unused variable: `i`
   --> src/main.rs:614:9
    |
614 |     for i in 0..n - 1 {
    |         ^ help: if this is intentional, prefix it with an underscore: `_i`

warning: variable `root` is assigned to, but never used
   --> src/main.rs:623:13
    |
623 |     let mut root = 0 as usize;
    |             ^^^^
    |
    = note: consider using `_root` instead

warning: value assigned to `root` is never read
   --> src/main.rs:626:13
    |
626 |             root = i;
    |             ^^^^
    |
    = help: maybe it is overwritten before being read?
    = note: `#[warn(unused_assignments)]` on by default

warning: variable does not need to be mutable
   --> src/main.rs:641:13
    |
641 |         let mut x = data[i] as i64;
    |             ----^
    |             |
    |             help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

ソースコード

diff #

// -*- coding:utf-8-unix -*-
// #![feature(map_first_last)]
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_macros)]
use std::collections::*;
use std::convert::*;
use std::convert::{From, Into};
use std::fmt::Debug;
use std::fs::File;
use std::io::prelude::*;
use std::io::*;
use std::marker::Copy;
use std::mem::*;
use std::ops::Bound::*;
use std::ops::{Add, Mul, Neg, Sub};
use std::str;
use std::vec;
use std::{cmp, process::Output};
use std::{cmp::Ordering, env::consts::DLL_PREFIX};
use std::{cmp::Ordering::*, f32::consts::PI};

const INF: i64 = 1223372036854775807;
const UINF: usize = INF as usize;
const FINF: f64 = 122337203685.0;
const INF128: i128 = 1223372036854775807000000000000;
const LINF: i64 = 2147483647;
// const MOD: i64 = 1000000007;
const MOD: i64 = 998244353;
const UMOD: usize = MOD as usize;
use std::cmp::*;
use std::collections::*;
use std::io::stdin;
use std::io::stdout;
use std::io::Write;

macro_rules! p {
    ($x:expr) => {
        println!("{}", $x);
    };
}

macro_rules! d {
    ($x:expr) => {
        dbg!($x);
    };
}

macro_rules! v {
    ($x:expr) => {
        if $x == 0 {
            vec![];
        } else if $x == 1 {
            BTreeSet::new();
        } else if $x == 2 {
            BTreeMap::new();
        }
    };
}

// use str::Chars;
#[allow(dead_code)]
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

#[allow(dead_code)]
fn readi() -> (i64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    iter.next().unwrap().parse::<i64>().unwrap()
}

#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
#[allow(dead_code)]
fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

#[allow(dead_code)]
fn readii() -> (i64, i64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
    )
}

fn readff() -> (f64, f64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<f64>().unwrap(),
        iter.next().unwrap().parse::<f64>().unwrap(),
    )
}

#[allow(dead_code)]
fn readiii() -> (i64, i64, i64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
    )
}
#[allow(dead_code)]
fn readuu() -> (usize, usize) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
    )
}

fn readcc() -> (char, char) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<char>().unwrap(),
        iter.next().unwrap().parse::<char>().unwrap(),
    )
}

#[allow(dead_code)]
fn readuuu() -> (usize, usize, usize) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
    )
}

#[allow(dead_code)]
fn readuuuu() -> (usize, usize, usize, usize) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
    )
}

fn readiiii() -> (i64, i64, i64, i64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
    )
}

macro_rules! M {
    (a :expr ) => {
        M::new({ a })
    };
}
#[derive(Copy, Clone)]
pub struct M(i64);
impl M {
    fn new(x: i64) -> Self {
        M(x.rem_euclid(MOD))
    }
    fn pow(self, n: usize) -> Self {
        match n {
            0 => M::new(1),
            _ => {
                let mut a = self.pow(n >> 1);
                a *= a;
                if n & 1 == 1 {
                    a *= self;
                }
                a
            }
        }
    }
    fn inv(self) -> Self {
        self.pow((MOD - 2) as usize)
    }
}
impl std::ops::Neg for M {
    type Output = M;
    fn neg(self) -> Self::Output {
        Self::new(-self.0)
    }
}
impl std::ops::AddAssign<M> for M {
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::AddAssign<i64> for M {
    fn add_assign(&mut self, rhs: i64) {
        *self += M::new(rhs);
    }
}
impl std::ops::AddAssign<usize> for M {
    fn add_assign(&mut self, rhs: usize) {
        *self += M::new(rhs as i64);
    }
}
impl<T> std::ops::Add<T> for M
where
    M: std::ops::AddAssign<T>,
{
    type Output = Self;
    fn add(self, other: T) -> Self {
        let mut res = self;
        res += other;
        res
    }
}
impl std::ops::SubAssign<M> for M {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
        if self.0 < 0 {
            self.0 += MOD;
        }
    }
}
impl std::ops::SubAssign<i64> for M {
    fn sub_assign(&mut self, rhs: i64) {
        *self -= M::new(rhs);
        if (*self).0 < 0 {
            self.0 += MOD;
        }
    }
}
impl std::ops::SubAssign<usize> for M {
    fn sub_assign(&mut self, rhs: usize) {
        *self -= M::new(rhs as i64);
        if (*self).0 < 0 {
            self.0 += MOD;
        }
    }
}
impl<T> std::ops::Sub<T> for M
where
    M: std::ops::SubAssign<T>,
{
    type Output = Self;
    fn sub(self, other: T) -> Self {
        let mut res = self;
        res -= other;
        res
    }
}
impl std::ops::MulAssign<M> for M {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 *= rhs.0;
        self.0 %= MOD;
    }
}
impl std::ops::MulAssign<i64> for M {
    fn mul_assign(&mut self, rhs: i64) {
        *self *= M::new(rhs);
    }
}
impl std::ops::MulAssign<usize> for M {
    fn mul_assign(&mut self, rhs: usize) {
        *self *= M::new(rhs as i64);
    }
}
impl<T> std::ops::Mul<T> for M
where
    M: std::ops::MulAssign<T>,
{
    type Output = Self;
    fn mul(self, other: T) -> Self {
        let mut res = self;
        res *= other;
        res
    }
}
impl std::ops::DivAssign<M> for M {
    fn div_assign(&mut self, rhs: Self) {
        *self *= rhs.inv();
    }
}
impl std::ops::DivAssign<i64> for M {
    fn div_assign(&mut self, rhs: i64) {
        *self /= M::new(rhs);
    }
}
impl std::ops::DivAssign<usize> for M {
    fn div_assign(&mut self, rhs: usize) {
        *self /= M::new(rhs as i64);
    }
}
impl<T> std::ops::Div<T> for M
where
    M: std::ops::DivAssign<T>,
{
    type Output = Self;
    fn div(self, other: T) -> Self {
        let mut res = self;
        res /= other;
        res
    }
}
impl std::fmt::Display for M {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::ops::Deref for M {
    type Target = i64;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl std::ops::DerefMut for M {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[allow(dead_code)]
pub fn gcd(a: usize, b: usize) -> usize {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}
#[allow(dead_code)]
pub fn lcm(a: usize, b: usize) -> usize {
    a / gcd(a, b) * b
}
#[allow(dead_code)]
/// (gcd, x, y)
pub fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (gcd, x, y) = extgcd(b, a % b);
        (gcd, y, x - (a / b) * y)
    }
}
#[allow(dead_code)]
/// x ^ n % m
pub fn mod_pow(x: usize, n: usize, m: usize) -> usize {
    let mut res = 1;
    let mut x = x % m;
    let mut n = n;
    while n > 0 {
        if n & 1 == 1 {
            res = (res * x) % m;
        }
        x = (x * x) % m;
        n >>= 1;
    }
    res
}
#[allow(dead_code)]
pub fn mod_inverse(a: usize, m: usize) -> usize {
    let (_, x, _) = extgcd(a as i64, m as i64);
    ((m as i64 + x) as usize % m) % m
}
#[allow(dead_code)]
pub fn fact_table(len: usize, m: usize) -> Vec<usize> {
    let mut res = vec![1; len + 1];
    for i in 1..len + 1 {
        res[i] = (i as usize * res[i - 1]) % m;
    }
    res
}
#[allow(dead_code)]
/// Factorial and Inverse factorial table
pub fn fact_inv_table(size: usize, m: usize) -> (Vec<usize>, Vec<usize>) {
    let mut fact = vec![1; size];
    let mut fact_inv = vec![1; size];
    for i in 2..size {
        fact[i] = fact[i - 1] * i as usize % m;
        fact_inv[i] = m - ((m / i as usize) * fact_inv[(m % i as usize) as usize] % m);
    }
    for i in 1..size {
        fact_inv[i] = fact_inv[i - 1] * fact_inv[i] % m;
    }
    (fact, fact_inv)
}
#[allow(dead_code)]
/// (a mod p, e when n! = a p\^e)
pub fn mod_fact(n: usize, p: usize, fact: &[usize]) -> (usize, usize) {
    if n == 0 {
        (1, 0)
    } else {
        let (a, b) = mod_fact(n / p, p, fact);
        let pow = b + n / p;
        if n / p % 2 != 0 {
            (a * (p - fact[(n % p) as usize]) % p, pow)
        } else {
            (a * fact[(n % p) as usize] % p, pow)
        }
    }
}
#[allow(dead_code)]
/// C(n, k) % p
pub fn mcom(n: usize, k: usize, fact: &[usize]) -> usize {
    let p = MOD as usize;
    if k == 0 {
        return 1;
    }
    if n < k {
        0
    } else {
        let (a1, e1) = mod_fact(n, p, fact);
        let (a2, e2) = mod_fact(k, p, fact);
        let (a3, e3) = mod_fact(n - k, p, fact);
        if e1 > e2 + e3 {
            0
        } else {
            a1 * mod_inverse(a2 * a3 % p, p) % p
        }
    }
}

pub fn mperm(n: usize, k: usize, fact: &[usize]) -> usize {
    let p = MOD as usize;
    if k == 0 {
        return 1;
    }
    if n < k {
        0
    } else {
        let (a1, e1) = mod_fact(n, p, fact);
        let (a2, e2) = mod_fact(k, p, fact);
        let (a3, e3) = mod_fact(n - k, p, fact);
        if e1 > e2 + e3 {
            0
        } else {
            a1 * mod_inverse(a3 % p, p) % p
        }
    }
}

pub fn hcom(n: usize, k: usize, fact: &[usize]) -> usize {
    return mcom(n + k - 1, k, fact);
}

pub struct Combination {
    m: usize,
    f_table: Vec<usize>,
}

impl Combination {
    // 0 <= size <= 10^8 is constrained.
    pub fn new(mod_num: usize, table_size: usize) -> Self {
        Self {
            m: mod_num,
            f_table: vec![0; table_size],
        }
    }
    pub fn build(&mut self) {
        let size = self.f_table.len();
        self.f_table = fact_table(size, self.m);
    }
    fn fact_table(len: usize, m: usize) -> Vec<usize> {
        let mut res = vec![1; len + 1];
        for i in 1..len + 1 {
            res[i] = (i as usize * res[i - 1]) % m;
        }
        res
    }

    pub fn p(&mut self, n: usize, k: usize) -> usize {
        let p = MOD as usize;
        if k == 0 {
            return 1;
        }
        if n < k {
            0
        } else {
            let (a1, e1) = mod_fact(n, p, &self.f_table);
            let (a2, e2) = mod_fact(k, p, &self.f_table);
            let (a3, e3) = mod_fact(n - k, p, &self.f_table);
            if e1 > e2 + e3 {
                0
            } else {
                a1 * mod_inverse(a3 % p, p) % p
            }
        }
    }
    pub fn c(&mut self, n: usize, k: usize) -> usize {
        let p = MOD as usize;
        if n == 0 {
            return 0;
        }
        if k == 0 {
            return 1;
        }
        if n < k {
            0
        } else {
            let (a1, e1) = mod_fact(n, p, &self.f_table);
            let (a2, e2) = mod_fact(k, p, &self.f_table);
            let (a3, e3) = mod_fact(n - k, p, &self.f_table);
            if e1 > e2 + e3 {
                0
            } else {
                a1 * mod_inverse(a2 * a3 % p, p) % p
            }
        }
    }
    pub fn h(&mut self, n: usize, k: usize) -> usize {
        return mcom(n + k - 1, k, &self.f_table);
    }

    pub fn factorial(&mut self, n: usize) -> usize {
        return self.p(n, n);
    }

    fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
        if b == 0 {
            (a, 1, 0)
        } else {
            let (gcd, x, y) = extgcd(b, a % b);
            (gcd, y, x - (a / b) * y)
        }
    }
    pub fn mod_inverse(a: usize, m: usize) -> usize {
        let (_, x, _) = extgcd(a as i64, m as i64);
        ((m as i64 + x) as usize % m) % m
    }
    fn mod_fact(&mut self, n: usize, p: usize, fact: &[usize]) -> (usize, usize) {
        if n == 0 {
            (1, 0)
        } else {
            let (a, b) = mod_fact(n / p, p, fact);
            let pow = b + n / p;
            if n / p % 2 != 0 {
                (a * (p - fact[(n % p) as usize]) % p, pow)
            } else {
                (a * fact[(n % p) as usize] % p, pow)
            }
        }
    }
}

fn djikstra(graph: &Vec<Vec<(usize, usize)>>, start: usize) -> Vec<usize> {
    let mut dist = vec![INF as usize; graph.len()];
    let mut heap = BinaryHeap::new();
    heap.push(Reverse((0 as usize, start)));
    dist[start] = 0;
    while let Some(Reverse(x)) = heap.pop() {
        let cost = x.0;
        let v = x.1;
        if cost > dist[v] {
            continue;
        }
        for edge in &graph[v] {
            let nc = cost + edge.1;
            let nv = edge.0;
            if nc < dist[nv] {
                heap.push(Reverse((nc, nv)));
                dist[nv] = nc;
            }
        }
    }
    return dist;
}

fn dfs(v: usize, graph: &Vec<Vec<(usize, usize)>>, used: &mut Vec<usize>, data: &mut Vec<usize>) {
    used[v] = 1;
    let mut res = 0 as usize;
    for i in graph[v].iter() {
        let nv = (*i).0;
        if used[nv] == 1 {
            continue;
        }
        dfs(nv, &graph, used, data);
        res += data[nv];
    }
    res += 1;
    data[v] = res;
    return;
}

fn solve() {
    let n: usize = read();
    let mut graph = vec![vec![(0 as usize, 0 as usize); (0) as usize]; (n) as usize];
    let mut data = vec![0; n];
    if n == 2 {
        println!("{:?}", 0);
        return;
    }
    for i in 0..n - 1 {
        let (mut a, mut b) = readuu();
        a -= 1;
        b -= 1;
        graph[a].push((b, 1));
        graph[b].push((a, 1));
        data[a] += 1;
        data[b] += 1;
    }
    let mut root = 0 as usize;
    for i in 0..n {
        if data[i] == 1 {
            root = i;
        }
    }
    // let mut d = djikstra(&graph, root);
    // d!(d.clone());
    let mut used = vec![0; n];
    let mut data = vec![0; n];
    dfs(0, &graph, &mut used, &mut data);
    let mut res = M(1);
    res *= M(((n as i64) * (n as i64 - 1) / 2) as i64);
    res *= M(n as i64 - 1);
    // println!("{:?}", data.clone());
    // println!("{:?}", res.0);

    for i in 1..n {
        let mut x = data[i] as i64;
        res -= M(n as i64 - x) * x;
    }
    // println!("{:?}", res.0);
    res /= M(((n as i64) * (n as i64 - 1) / 2) as i64);
    res /= M(n as i64 - 1);
    println!("{:?}", res.0);
    return;
}

fn main() {
    solve();
}
0