51nod 1353 树
2024-10-20 13:43:36
树背包
设f[i][j]表示第i个点,和子节点组成的联通块大小为j,其他都可行的方案
j=0表示可行的总方案
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int mod=1e9+;
int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(''<=ch&&ch<=''){x=x*+ch-'';ch=getchar();}
return x;
} int n,K;
struct node
{
int x,y,next;
}a[];int len,last[];
void ins(int x,int y)
{
len++;
a[len].x=x;a[len].y=y;
a[len].next=last[x];last[x]=len;
}
int f[][],tot[];
void dfs(int x,int fr)
{
f[x][]=;tot[x]=;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y!=fr)
{
dfs(y,x);
for(int i=tot[x];i>=;i--)
{
for(int j=tot[y];j>=;j--)
f[x][i+j]=(f[x][i+j]+((LL)f[x][i]*(LL)f[y][j])%mod)%mod;
f[x][i]=((LL)f[x][i]*(LL)f[y][])%mod;
}
tot[x]+=tot[y];
}
}
for(int i=K;i<=tot[x];i++)f[x][]=(f[x][]+f[x][i])%mod;
} int main()
{
int x,y;
n=read(),K=read();
len=;memset(last,,sizeof(last));
for(int i=;i<n;i++)
{
x=read(),y=read();
ins(x,y),ins(y,x);
}
memset(f,,sizeof(f));
dfs(,);
printf("%d\n",f[][]);
return ;
}
最新文章
- Mac 下 WebStorm 配置go语言开发环境
- 在CSDN中添加友情连接
- spring:bean的定义
- mysql 启动错误1026
- 重学STM32----(二)
- silverlight 获取路径 config
- Linux Shell 脚本
- Spark学习笔记-如何运行wordcount(使用jar包)
- MySql连接问题
- 201521123025《java程序设计》第14周学习总结
- 安卓java.lang.IllegalStateException: The specified child already has a parent.解决方案
- mysql中replace替换字符串更改方法
- mysql处理重复数据
- 2.docker的网络模式
- k8s 英文文档翻译
- rac 关库 启库
- 用C#创建XML, XML格式化输出
- java设计模式-----21、备忘录模式
- model方法取值总结
- mac 系统安装VM虚拟机打开时报错,提示不是虚拟磁盘的解决方式。