結果
| 問題 |
No.196 典型DP (1)
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2020-01-10 17:13:00 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 17 ms / 2,000 ms |
| コード長 | 5,192 bytes |
| コンパイル時間 | 22,521 ms |
| コンパイル使用メモリ | 378,276 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-23 22:06:14 |
| 合計ジャッジ時間 | 24,644 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 41 |
ソースコード
// ---------- begin ModInt ----------
const MOD: u32 = 1_000_000_007;
#[derive(Clone, Copy)]
struct ModInt(u32);
impl std::ops::Add for ModInt {
type Output = ModInt;
fn add(self, rhs: ModInt) -> Self::Output {
let mut d = self.0 + rhs.0;
if d >= MOD {
d -= MOD;
}
ModInt(d)
}
}
impl std::ops::AddAssign for ModInt {
fn add_assign(&mut self, rhs: ModInt) {
*self = *self + rhs;
}
}
impl std::ops::Sub for ModInt {
type Output = ModInt;
fn sub(self, rhs: ModInt) -> Self::Output {
let mut d = self.0 + MOD - rhs.0;
if d >= MOD {
d -= MOD;
}
ModInt(d)
}
}
impl std::ops::SubAssign for ModInt {
fn sub_assign(&mut self, rhs: ModInt) {
*self = *self - rhs;
}
}
impl std::ops::Mul for ModInt {
type Output = ModInt;
fn mul(self, rhs: ModInt) -> Self::Output {
ModInt((self.0 as u64 * rhs.0 as u64 % MOD as u64) as u32)
}
}
impl std::ops::MulAssign for ModInt {
fn mul_assign(&mut self, rhs: ModInt) {
*self = *self * rhs;
}
}
impl std::ops::Neg for ModInt {
type Output = ModInt;
fn neg(self) -> Self::Output {
ModInt(if self.0 == 0 {0} else {MOD - self.0})
}
}
impl std::fmt::Display for ModInt {
fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::str::FromStr for ModInt {
type Err = std::num::ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let val = s.parse::<u32>()?;
Ok(ModInt::new(val))
}
}
#[allow(dead_code)]
impl ModInt {
pub fn new(n: u32) -> ModInt {
ModInt(n % MOD)
}
pub fn zero() -> ModInt {
ModInt(0)
}
pub fn one() -> ModInt {
ModInt(1)
}
pub fn pow(self, mut n: u32) -> ModInt {
let mut t = ModInt::one();
let mut s = self;
while n > 0 {
if n & 1 == 1 {
t *= s;
}
s *= s;
n >>= 1;
}
t
}
pub fn inv(self) -> ModInt {
self.pow(MOD - 2)
}
}
// ---------- end ModInt ----------
// ---------- begin Tree DP ----------
struct TreeDP<Edge, Value, Init, Merge> {
size: usize,
graph: Vec<Vec<(usize, Edge)>>,
init: Init,
merge: Merge,
phantom: std::marker::PhantomData<Value>,
}
impl<Edge, Value, Init, Merge> TreeDP<Edge, Value, Init, Merge>
where Edge: Clone,
Value: Clone,
Init: Fn(usize) -> Value,
Merge: Fn(Value, Value, &Edge) -> Value,
{
fn new(size: usize, init: Init, merge: Merge) -> Self {
TreeDP {
size: size,
graph: vec![vec![]; size],
init: init,
merge: merge,
phantom: std::marker::PhantomData,
}
}
fn add_edge(&mut self, a: usize, b: usize, c: Edge) {
assert!(a < self.size && b < self.size && a != b);
self.graph[a].push((b, c.clone()));
self.graph[b].push((a, c));
}
fn solve(&self, root: usize) -> Value {
let size = self.size;
let graph = &self.graph;
let mut topo = vec![];
let mut parent = vec![root; size];
let mut stack = vec![root];
while let Some(v) = stack.pop() {
topo.push(v);
for e in graph[v].iter() {
if e.0 != parent[v] {
parent[e.0] = v;
stack.push(e.0);
}
}
}
assert!(topo.len() == size);
let mut dp: Vec<Option<Value>> = (0..size).map(|_| None).collect();
for &v in topo.iter().rev() {
let mut now = (self.init)(v);
for u in graph[v].iter() {
if u.0 == parent[v] {
continue;
}
let b = dp[u.0].take().unwrap();
now = (self.merge)(now, b, &u.1);
}
dp[v] = Some(now);
}
dp[root].take().unwrap()
}
}
// ---------- end Tree DP ----------
use std::io::Read;
fn run() {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let mut it = s.trim().split_whitespace();
let n: usize = it.next().unwrap().parse().unwrap();
let k: usize = it.next().unwrap().parse().unwrap();
type Value = Vec<ModInt>;
type Edge = ();
let init = |_v: usize| -> Value {
vec![ModInt::one(), ModInt::one()]
};
let merge = |a: Value, b: Value, _c: &Edge| -> Value {
let mut next = vec![ModInt::zero(); a.len() + b.len() - 1];
for (i, x) in a.iter().enumerate().rev().skip(1) {
for (next, y) in next[i..].iter_mut().zip(b.iter()) {
*next += *x * *y;
}
}
*next.last_mut().unwrap() += ModInt::one();
next
};
let mut solver = TreeDP::new(n, init, merge);
for _ in 1..n {
let a: usize = it.next().unwrap().parse().unwrap();
let b: usize = it.next().unwrap().parse().unwrap();
solver.add_edge(a, b, ());
}
let ans = solver.solve(0)[k];
println!("{}", ans);
}
fn main() {
run();
}
akakimidori