CF 161D Distance in Tree 树形DP
一棵树,边长都是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的更多相关文章
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- 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 ...
- VK Cup 2012 Round 1 D. Distance in Tree (树形dp)
题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...
- CF 161D Distance in Tree【树DP】
题目大意:给一棵树,求树上两点之间距离为K的点对数目. 方程含义: dp(i,j)表示从已经遍历过的点到当前点i,路径长度为 j 的路径条数.因此,对于当前点,每当遍历了其中一个儿子节点的时候,首先统 ...
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- 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 ...
- 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: ...
- CF23 E. Tree 树形dp+高精度
题目链接 CF23 E. Tree 题解 CF竟让卡常QAQ dp+高精度 dp[x][j]表示以x为根的子树,x所属的联通块大小为j,的最大乘积(不带j这块 最后f[x]维护以x为根的子树的最大答案 ...
随机推荐
- mount挂载错误
错误信息: usa@usa-desktop:~/svn/aispeech/speechplatform/trunk/proxy/src$ sudo mount -t tmpfs -o size=200 ...
- 【深入Cocos2d-x】使用MVC架构搭建游戏Four
喜欢Four这个项目,就赶快在GitHub上Star这个项目吧! 喜欢我的文章,来微博关注我吧:王选易在学C艹 点我下载 项目起源 项目Logo: 下面是该游戏的项目地址,各位想参考源代码的同学可以到 ...
- C#基础:Lambda表达式
从委托的角度来看,Lambda表达式与匿名方法没有区别.在[C#基础:匿名方法]一文中,我使用了匿名方法来调用List<T>的FindAll方法.从C# 3.0开始,在使用匿名方法的地方, ...
- Knockout JS 入门示例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...
- shell之条件表达式
conditional expressions are used by the [[ compound command and the test and [ builtin commands. ari ...
- IDEA快捷键大全
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment
Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment 原文地址 我们也可以创建另外一种碎片——DialogFragment.顾名思义,DialogFragment就 ...
- openstack(liberty): devstack之stack.sh分析
学习openstack,从devstack入手,是个不错的选择.devstack中,首先需要分析stack.sh都做了些什么! 这里面涉及到了很多shell的基础知识.我就做个简单的梳理,方便后续查阅 ...
- 超强封装的RichTextBox控件(C#源码)
有点类似QQ聊天框所带的RichText. 功能进行了RTF的封装,直接调用函数插入图片,连接,特列文字.具体请查看代码 ExRichTextBox_src
- 【shell】通配符
‘’与“” [root@andon ~]# name='$date' [root@andon ~]# echo $name $date [root@andon ~]# name=abc [root@a ...