CodeForces 161D Distance in Tree【树形DP】
<题目链接>
题目大意:
一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define N int(5e4+10)
int n,k,ans,cnt;
int dp[N][],head[N];
struct Edge{
int to,nxt;
}edge[N<<];
void init(){
cnt=;ans=;
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
}
void addedge(int u,int v){
edge[cnt].to=v,edge[cnt].nxt=head[u];
head[u]=cnt++;
}
void dfs(int u,int fa){
dp[u][]=; //dp[i][j]表示以u为根的子树中,距u的距离为j的点的数量
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa)continue;
dfs(v,u);
for(int j=;j<k;j++)ans+=dp[u][j]*dp[v][k-j-];
for(int j=;j<=k;j++)dp[u][j]+=dp[v][j-];
}
}
int main(){
while(~scanf("%d%d",&n,&k)){
init();
for(int i=;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
addedge(u,v);addedge(v,u);
}
dfs(,-);
printf("%d\n",ans);
}
}
2019-02-07
CodeForces 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 161D Distance in Tree 树形DP
一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...
- VK Cup 2012 Round 1 D. Distance in Tree (树形dp)
题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- Codeforces 161D Distance in Tree(树的点分治)
题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...
- codeforces 416B. Appleman and Tree 树形dp
题目链接 Fill a DP table such as the following bottom-up: DP[v][0] = the number of ways that the subtree ...
- Codeforces 161D Distance in Tree
题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> # ...
随机推荐
- Confluence 6 通过 SSL 或 HTTPS 运行 - 为 HTTPS 修改你的 Confluence 基础 URL
在你的浏览器中,进入 > 基本配置(General Configuration). 单击 编辑(Edit). 修改服务器的基础 URL 为 HTTPS.请参考文档 configuring t ...
- Confluence 6 后台中的选择站点首页
后台中的选择站点首页选择项. https://www.cwiki.us/display/CONFLUENCEWIKI/Configuring+the+Site+Home+Page
- Confluence 6 编辑一个站点装饰文件
希望编辑一个站点的 decorator 文件: 进入 > 基本配置(General Configuration) > 布局(Layouts )(在Look and Feel 菜单下面) ...
- pod 使用详解
cd 进去到 项目目录 包含 xcodeproj 结尾的目录下 1 pod init 创建一个pod 文件 2 打开生产的pod 文件 然后 配置pod 文件 并保存 3 pod install 安 ...
- (一)python 数据模型
1.通过实现特殊方法,自定义类型可以表现的跟内置类型一样: 如下代码,实现len, getitem,可使自定义类型表现得如同列表一样. import collections from random i ...
- eclipse php pdt插件安装
安装动态语言工具包: help->new install software->work with 框输入 http://download.eclipse.org/technology/dl ...
- 项目中使用sass预处理器
安装sass npm install node-sass sass-loader --save 新建样式文件后缀为 .scss 在使用样式的页面引入:import 'xx.scss';
- java----微服务架构
参考文档 https://topsale.gitbooks.io/java-cloud-dubbo/content/ 单体应用: 项目的架构完完全全属于传统的 MVC 架构,所有的子系统都集成在一个很 ...
- Centos6.10部署TeamViewer
1.在官网下载支持Linux系统的包,建议下载TeamViewer12的包,官网URL:https://www.teamviewer.com/cn/download/linux/ 2.将下载的软件包导 ...
- jenkins权限管理,实现不同用户组显示对应视图views中不同的jobs
如何分组管理权限,如何实现不同用户组显示对应视图views中不同的jobs,建议使用Role Strategy Plugin插件. 1.安装Role Strategy Plugin插件. 2.“系统管 ...