結果

問題 No.1227 I hate ThREE
ユーザー o2co2c
提出日時 2020-09-15 10:51:50
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 8,238 bytes
コンパイル時間 10,928 ms
コンパイル使用メモリ 402,152 KB
実行使用メモリ 28,544 KB
最終ジャッジ日時 2024-06-22 01:37:52
合計ジャッジ時間 12,435 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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];
    let (d, s) = {
        let mut root = Rerooting::<D>::new(n);
        for _ in 0..n - 1 {
            let (u, v) = read!(usize1, usize1);
            to[u].push(v);
            to[v].push(u);
            root.add_edge(u, v);
        }
        root.init();
        let mut dm = n;
        let mut s = 0;
        for i in 0..n {
            if dm > root.ans[i] {
                dm = root.ans[i];
                s = i;
            }
        }
        (dm - 1, s)
    };

    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, cm, &mut dp, &mut visit);
    }

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

struct D;
impl DP for D {
    type T = usize;
    fn add_root(a: &Self::T) -> Self::T {
        a + 1
    }
    fn id() -> Self::T {
        0
    }
    fn concat(l: &Self::T, r: &Self::T) -> Self::T {
        std::cmp::max(*l, *r)
    }
}

trait DP {
    type T: Clone;
    fn add_root(a: &Self::T) -> Self::T;
    fn id() -> Self::T;
    fn concat(l: &Self::T, r: &Self::T) -> Self::T;
}
struct Rerooting<D: DP> {
    to: Vec<Vec<usize>>,
    dp: Vec<Vec<D::T>>,
    ans: Vec<D::T>,
}
impl<D: DP> Rerooting<D> {
    fn new(n: usize) -> Self {
        Rerooting {
            to: vec![Vec::<usize>::new(); n],
            dp: vec![Vec::<D::T>::new(); n],
            ans: vec![D::id(); n],
        }
    }
    fn add_edge(&mut self, u: usize, v: usize) {
        self.to[u].push(v);
        self.to[v].push(u);
    }
    fn init(&mut self) {
        self.dfs(0, None);
        self.bfs(0, D::id(), None);
    }
    fn dfs(&mut self, v: usize, p: Option<usize>) -> D::T {
        let mut dp_sum = D::id();
        let deg = self.to[v].len();
        self.dp[v] = vec![D::id(); deg];
        for i in 0..deg {
            let u = self.to[v][i];
            if Some(u) == p {
                continue;
            }
            self.dp[v][i] = self.dfs(u, Some(v));
            dp_sum = D::concat(&dp_sum, &self.dp[v][i]);
        }
        D::add_root(&dp_sum)
    }
    fn bfs(&mut self, v: usize, dp_p: D::T, p: Option<usize>) {
        if let Some(p) = p {
            let x = self.to[v].iter().enumerate().find(|&(_, v)| *v == p);
            if let Some((i, _)) = x {
                self.dp[v][i] = dp_p;
            }
        }
        let deg = self.to[v].len();
        let mut dp_sum_l = vec![D::id(); deg + 1];
        for i in 0..deg {
            dp_sum_l[i + 1] = D::concat(&dp_sum_l[i], &self.dp[v][i]);
        }
        let mut dp_sum_r = vec![D::id(); deg + 1];
        for i in (0..deg).rev() {
            dp_sum_r[i] = D::concat(&dp_sum_r[i + 1], &self.dp[v][i]);
        }
        self.ans[v] = D::add_root(&dp_sum_l[deg]);
        for i in 0..deg {
            let u = self.to[v][i];
            if Some(u) == p {
                continue;
            }
            self.bfs(
                u,
                D::add_root(&D::concat(&dp_sum_l[i], &dp_sum_r[i + 1])),
                Some(v),
            );
        }
    }
}

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

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