<题目链接>

题目大意:
一颗无向无环树,有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】的更多相关文章

  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 161D Distance in Tree 树形DP

    一棵树,边长都是1,问这棵树有多少点对的距离刚好为k 令tree(i)表示以i为根的子树 dp[i][j][1]:在tree(i)中,经过节点i,长度为j,其中一个端点为i的路径的个数dp[i][j] ...

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

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

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

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

  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 ...

  6. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

  7. Codeforces 161D Distance in Tree(树的点分治)

    题目大概是,给一棵树,统计距离为k的点对数. 不会DP啊..点分治的思路比较直观,啪啪啪敲完然后AC了.具体来说是这样的: 树上任何两点的路径都可以看成是一条过某棵子树根的路径,即任何一条路径都可以由 ...

  8. 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 ...

  9. Codeforces 161D Distance in Tree

    题目大意:给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500) 思路:树分治! #include<cstdio> # ...

随机推荐

  1. Eclipse 软件 Java 解决:出现的editor does not contain a main type错误框 问题

    Eclipse 软件 解决:出现的 editor does not contain a main type 错误框 问题 当你运行 Java文件是,如果弹出了下面的 错误框: 出现错误的原因: 当前的 ...

  2. Confluence 6 用户目录图例 - 连接 Jira

    上面的图:Confluence 连接到 JIRA 为用户管理. https://www.cwiki.us/display/CONFLUENCEWIKI/Diagrams+of+Possible+Con ...

  3. Linux超级守护进程——xinetd

    一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的. ...

  4. pytorch 参数初始化

    https://blog.csdn.net/daydayjump/article/details/80899029

  5. Java语法基础常见疑惑解答8,16,17,21图片补充

    8. 16. 17. 21

  6. vue 中axios 的基本配置和基本概念

    axios的基本概念及安装配置方法   ajax:异步请求,是一种无需再重新加载整个网页的情况下,能够更新部分网页的技术 axios:用于浏览器和node.js的基于promise的HTTP客户端 a ...

  7. 【python】json中字典key不可为数值型

    遇到了一个很诡异的错误.写一个字典,存入json文件.之后读出判断是否存在key.结果惊奇的发现,同一个key居然存在两次. 原因:json会将数值key转换为unicode 结论:使用json时字典 ...

  8. 【kafka】confluent_kafka重置offset

    之前写过两篇关于重置offset的博文,后来使用过程中都有问题. 经过各种尝试,终于找到了解决方案. 直接上代码: # coding=utf8 from confluent_kafka import ...

  9. SQLmap超详细文档和实例演示

    第一部分,使用文档的说明 Options(选项): -h, -–help 显示此帮助消息并退出 -hh 显示更多帮助信息并退出 –-version 显示程序的版本号并退出 -v VERBOSE 详细级 ...

  10. tensorflow(3):神经网络优化(ema,regularization)

    1.指数滑动平均 (ema) 描述滑动平均: with tf.control_dependencies([train_step,ema_op]) 将计算滑动平均与 训练过程绑在一起运行 train_o ...