結果

問題 No.1227 I hate ThREE
ユーザー o2co2c
提出日時 2020-09-15 10:17:02
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 6,095 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 150,716 KB
実行使用メモリ 28,608 KB
最終ジャッジ日時 2023-09-04 01:33:54
合計ジャッジ時間 3,998 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

pub fn readln() -> String {
    let mut line = String::new();
    ::std::io::stdin()
        .read_line(&mut line)
        .unwrap_or_else(|e| panic!("{}", e));
    line
}
macro_rules !read {($($t :tt ) ,*;$n :expr ) =>{{let stdin =::std ::io ::stdin () ;let ret =::std ::io ::BufRead ::lines (stdin .lock () ) .take ($n ) .map (|line |{let line =line .unwrap () ;let mut it =line .split_whitespace () ;_read !(it ;$($t ) ,*) } ) .collect ::<Vec <_ >>() ;ret } } ;($($t :tt ) ,*) =>{{let line =readln () ;let mut it =line .split_whitespace () ;_read !(it ;$($t ) ,*) } } ;}
macro_rules !_read {($it :ident ;[char ] ) =>{_read !($it ;String ) .chars () .collect ::<Vec <_ >>() } ;($it :ident ;[u8 ] ) =>{Vec ::from (_read !($it ;String ) .into_bytes () ) } ;($it :ident ;usize1 ) =>{$it .next () .unwrap_or_else (||panic !("input mismatch" ) ) .parse ::<usize >() .unwrap_or_else (|e |panic !("{}" ,e ) ) -1 } ;($it :ident ;[usize1 ] ) =>{$it .map (|s |s .parse ::<usize >() .unwrap_or_else (|e |panic !("{}" ,e ) ) -1 ) .collect ::<Vec <_ >>() } ;($it :ident ;[$t :ty ] ) =>{$it .map (|s |s .parse ::<$t >() .unwrap_or_else (|e |panic !("{}" ,e ) ) ) .collect ::<Vec <_ >>() } ;($it :ident ;$t :ty ) =>{$it .next () .unwrap_or_else (||panic !("input mismatch" ) ) .parse ::<$t >() .unwrap_or_else (|e |panic !("{}" ,e ) ) } ;($it :ident ;$($t :tt ) ,+) =>{($(_read !($it ;$t ) ) ,*) } ;}

fn main() {
    let (n, c) = read!(usize, usize);
    let mut to = vec![vec![]; n];
    for _ in 0..n - 1 {
        let (u, v) = read!(usize1, usize1);
        to[u].push(v);
        to[v].push(u);
    }

    let (s, d) = {
        let mut d = vec![0; n];
        dfs_g(0, 0, &to, n, &mut d);
        let mut s = 0;
        let mut dn = n;
        for i in 0..n {
            if d[i] < dn {
                dn = d[i];
                s = i;
            }
        }
        (s, dn)
    };

    let cm = std::cmp::min(c, 6 * d + 1);
    let mut dp = vec![vec![Mint(1); cm + 1]; n];
    let mut visit = vec![vec![false; cm + 1]; n];
    let mut ans = Mint(0);
    for i in 1..=cm {
        ans += dfs(s, s, &to, i as i64, cm, &mut dp, &mut visit);
    }

    if cm != c {
        ans += dp[s][3 * d + 1] * (c - (6 * n + 1)) as i64;
    }
    println!("{}", ans);
}

fn dfs_g(u: usize, p: usize, to: &Vec<Vec<usize>>, n: usize, d: &mut Vec<usize>) -> usize {
    let mut x = 0;
    for &v in to[u].iter() {
        if v == p {
            continue;
        }
        let c = dfs_g(v, u, to, n, d);
        d[u] = std::cmp::max(d[u], c);
        x += c;
    }
    d[u] = std::cmp::max(d[u], n - x);
    x + 1
}

fn dfs(
    u: usize,
    p: usize,
    to: &Vec<Vec<usize>>,
    s: i64,
    c: usize,
    dp: &mut Vec<Vec<Mint>>,
    visit: &mut Vec<Vec<bool>>,
) -> Mint {
    if s <= 0 || s > c as i64 {
        return Mint(0);
    }
    let s = s as usize;
    if visit[u][s] {
        return dp[u][s];
    }
    visit[u][s] = true;
    let mut x = Mint(1);
    for &v in to[u].iter() {
        if v == p {
            continue;
        }
        x *= dfs(v, u, to, s as i64 - 3, c, dp, visit) + dfs(v, u, to, s as i64 + 3, c, dp, visit);
    }
    dp[u][s] = x;
    x
}

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