一棵树,边长都是1,问这棵树有多少点对的距离刚好为k

令tree(i)表示以i为根的子树

dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数
dp[i][j][0]:在tree(i)中,经过节点i,长度为j,端点不在i的路径的个数

则目标:∑(dp[i][k][0]+dp[i][k][1])
初始化:dp[i][0][1]=1,其余为0

siz[i]:tree(i)中,i与离i最远的点的距离
递推:
dp[i][j][0]+=dp[i][j-l][1]*dp[soni][l-1][1]
dp[i][j][1]=∑dp[soni][j-1][1]

注意:在更新每一个dp[i]时,先更新dp[i][j][0],再更新dp[i][j][1]

 #include<cstdio>
#include<cstring>
#include<iostream> using namespace std; const int maxn=+;
const int maxk=;
#define LL long long inline int max(int a,int b)
{
return a>b?a:b;
} inline int min(int a,int b)
{
return a<b?a:b;
} LL dp[maxn][maxk][];
int siz[maxn];
struct Edge
{
int to,next;
};
Edge edge[maxn<<];
int head[maxn];
int tot;
int k; void init()
{
memset(head,-,sizeof head);
tot=;
memset(dp,,sizeof dp);
} void addedge(int u,int v)
{
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
} void solve(int ,int );
void dfs(int ,int ); int main()
{
init();
int n;
scanf("%d %d",&n,&k);
for(int i=;i<n;i++)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n,k);
return ;
} void solve(int n,int k)
{
dfs(,);
LL ans=;
for(int i=;i<=n;i++)
{
ans+=(dp[i][k][]+dp[i][k][]);
}
cout<<ans<<endl;
return ;
} void dfs(int u,int pre)
{
dp[u][][]=;
siz[u]=;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].to;
if(v==pre)
continue;
dfs(v,u);
for(int l=;l<=siz[v]+;l++)
{
for(int j=l+;j<=siz[u]+l;j++)
{
if(j>k)
continue;
dp[u][j][]+=dp[u][j-l][]*dp[v][l-][];
}
}
for(int j=;j<=siz[v]+;j++)
dp[u][j][]+=dp[v][j-][];
siz[u]=max(siz[u],siz[v]+);
siz[u]=min(siz[u],k);
}
}

CF 161D Distance in Tree 树形DP的更多相关文章

  1. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  2. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  3. VK Cup 2012 Round 1 D. Distance in Tree (树形dp)

    题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...

  4. CF 161D Distance in Tree【树DP】

    题目大意:给一棵树,求树上两点之间距离为K的点对数目. 方程含义: dp(i,j)表示从已经遍历过的点到当前点i,路径长度为 j 的路径条数.因此,对于当前点,每当遍历了其中一个儿子节点的时候,首先统 ...

  5. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  6. Codeforces 161D Distance in Tree(树型DP)

    题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...

  7. CF 337D Book of Evil 树形DP 好题

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  8. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  9. CF23 E. Tree 树形dp+高精度

    题目链接 CF23 E. Tree 题解 CF竟让卡常QAQ dp+高精度 dp[x][j]表示以x为根的子树,x所属的联通块大小为j,的最大乘积(不带j这块 最后f[x]维护以x为根的子树的最大答案 ...

随机推荐

  1. spark优化之优化数据结构

    概序: 要减少内存的消耗,除了使用高效的序列化类库以外,还有一个很重要的事情,就是优化数据结构.从而避免Java语法特性中所导致的额外内存的开销,比如基于指针的Java数据结构,以及包装类型. 有一个 ...

  2. 适配IOS9中间遇到的一些问题

    1 directory not found for option问题 警告如下: ld: warning: directory not found for option ‘-F/Application ...

  3. C 语言中 free() 函数简单分析

    又是一个睡不着的夜晚,现在是凌晨03:16,不知道是不是感冒的原因,头脑并不是清醒,但是就是睡不着.摸着黑打开电脑,洗了杯子抓了点茶叶,然后打开饮水机电源.舍友们都睡着了,我戴着耳机听着轻音乐,也能听 ...

  4. Android项目——短信发送器

    因为应用要使用手机的短信服务,所以要在清单文件AndroidManifest.xml中添加短信服务权限: <?xml version="1.0" encoding=" ...

  5. Checking the content of the pointer

    https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/d481b7ec-cd67-476c-b2d7-ca3334effa16/h ...

  6. vb6动态创建webbrowser

    Option Explicit Private WithEvents IE As VBControlExtender Private Sub Command1_Click() Dim IE Set I ...

  7. myBatis 参数配置

    关于参数的类型 有四种类型: http://www.07net01.com/zhishi/402787.html 以上链接有所介绍,四种类型有:单个参数.多个参数.Map封装参数.List封装IN 如 ...

  8. python compile

    compile(source, filename, mode[, flags[, dont_inherit]]) 参数source:字符串或者AST(Abstract Syntax Trees)对象. ...

  9. CentOS 6.6 yum 方式安装sunversion 服务器

    检查已安装版本 #检查是否安装了低版本的SVN[root@localhost /]# rpm -qa subversion #如果存储旧版本,卸载旧版本SVN[root@localhost modul ...

  10. Myisam and InnoDB

    MyISAM引擎的特点: 1.堆组织表:2.不支持事务:3.数据文件和索引文件分开存储:4.支持全文索引:5.主键索引和二级索引完全一样都是B+树的数据结构,只有是否唯一的区别(主键和唯一索引有唯一属 ...