一棵树,边长都是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. Jquery实现图片的预加载与延时加载

    有很多项目经常会需要判断图片加载完成后执行相应的操作,或者需要图片延迟加载,网上虽然已经有很不错的插件,但要为这些效果还得单独加载一个插件的话总感觉有点不舒服,干脆自己写了个方法: 1 2 3 4 5 ...

  2. Amazon-countDuplicate

    Print the count of duplicate char in a given string in same order. Ex: Input- 'abbaccdbac', Output- ...

  3. Linux comands

    https://www.mankier.com/8/vmstat http://docs.oracle.com/cd/E23824_01/html/821-1451/spmonitor-22.html ...

  4. 机器学习&人工智能书籍

    Introduction to Machine Learning https://www.amazon.cn/Introduction-to-Machine-Learning-Alpaydin-Eth ...

  5. 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版!

    转--http://www.2cto.com/kf/201402/277535.html 万能js实现翻页,动态生成内容自动翻页,兼容各种浏览器(已测试)----神器版! 2014-02-11     ...

  6. js button onclick动作赋值操作

    昨天遇到的小问题 记录下 主要的东西其实都在这里:http://www.jb51.net/article/35107.htm 我稍微写一下: <script> function show( ...

  7. HTML,XML中的转义字符

    HTML中的转义字符 HTML中<, >,&等有特别含义,(前两个字符用于链接签,&用于转义),不能直接使用.使用这三个字符时,应使用他们的转义序列,如下所示: & ...

  8. 013. asp.net统计网站访问人数

    Global.asax中的代码: <%@ Application Language="C#" %> <script runat="server" ...

  9. shell脚本实例-系统监控

    shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...

  10. unity,生成的mac版游戏切场景时卡死解法

    unity版本为5.1.1,在编辑器里运行没问题,build出的windows版运行也没问题,但build出的mac版在个别场景切换时会卡死,通过查看log(查看build版本log的方法参考:htt ...