[CF161.D] Distance in Tree
time limit per test

3 seconds

memory limit per test

512 megabytes

A tree is a connected graph that doesn't contain any cycles.

The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.

You are given a tree with n vertices and a positive number k. Find the number of distinct pairs of the vertices which have a distance of exactly k between them. Note that pairs (vu) and (u,v) are considered to be the same pair.

Input

The first line contains two integers n and k (1 ≤ n ≤ 50000, 1 ≤ k ≤ 500) — the number of vertices and the required distance between the vertices.

Next n - 1 lines describe the edges as "ai bi" (without the quotes) (1 ≤ ai, bi ≤ nai ≠ bi), where ai and bi are the vertices connected by the i-th edge. All given edges are different.

Output

Print a single integer — the number of distinct pairs of the tree's vertices which have a distance of exactly k between them.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
inpu
  1. 5 2
    1 2
    2 3
    3 4
    2 5
output
  1. 4
input
  1. 5 3
    1 2
    2 3
    3 4
    4 5
output
  1. 2
Note

In the first sample the pairs of vertexes at distance 2 from each other are (1, 3), (1, 5), (3, 5) and (2, 4).

题目大意:树上有N个点,问多少对不同点对(u,v)最短路为K?

试题分析:设dp[N][K]代表从i走j步能到达多少点。

     初始化:dp[i][0]=1;//它不走可以到它自己

     转移一步:dp[i][j]=sum(dp[i->son][j-1]);

     统计答案分两步,一步是从i走K步能到达的点:dp[i][K]

     一步是以i为最近公共祖先的点对:dp[i->son][t-1]*(dp[i][K-t]-dp[i->son][K-t-1]);

     因为u,v   v,u算一对,所以ans最后加上tmp/2;

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<vector>
  5. #include<queue>
  6. #include<stack>
  7. #include<algorithm>
  8. using namespace std;
  9.  
  10. inline int read(){
  11. int x=0,f=1;char c=getchar();
  12. for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
  13. for(;isdigit(c);c=getchar()) x=x*10+c-'0';
  14. return x*f;
  15. }
  16. const int MAXN=100001;
  17. const int INF=999999;
  18. int N,K;
  19. long long dp[50001][501];
  20. vector<int> vec[50001];
  21. long long ans;
  22.  
  23. void dfs(int x,int fa){
  24. dp[x][0]=1;
  25. for(int i=0;i<vec[x].size();i++){
  26. if(vec[x][i]==fa) continue;
  27. dfs(vec[x][i],x);
  28. }
  29. for(int i=0;i<vec[x].size();i++){
  30. if(vec[x][i]==fa) continue;
  31. for(int j=1;j<=K;j++) dp[x][j]+=dp[vec[x][i]][j-1];
  32. }
  33. ans+=dp[x][K]; long long tmp=0;
  34. for(int i=0;i<vec[x].size();i++){
  35. if(vec[x][i]!=fa)
  36. for(int j=1;j<K;j++) tmp+=(dp[vec[x][i]][j-1]*(dp[x][K-j]-dp[vec[x][i]][K-j-1]));
  37. }
  38. ans+=(tmp/2);
  39. return ;
  40. }
  41.  
  42. int main(){
  43. N=read(),K=read();
  44. for(int i=1;i<N;i++){
  45. int u=read(),v=read();
  46. vec[u].push_back(v);
  47. vec[v].push_back(u);
  48. }
  49. dfs(1,-1);
  50. printf("%d\n",ans);
  51. }

【树形dp】Distance in Tree的更多相关文章

  1. 【树形dp】Apple Tree

    [poj2486]Apple Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10800   Accepted: 3 ...

  2. codeforces 161D Distance in Tree 树形dp

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

  3. 【树形dp】【CF161D】distance on a tree + 【P1352】没有上司的舞会

    T1题面: 输入点数为N一棵树 求树上长度恰好为K的路径个数 (n < 1e5, k < 500) 这是今天的考试题,也是一道假的紫题,因为我一个根本不会dp的蒟蒻只知道状态就一遍A掉了- ...

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

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

  5. [codeforces161D]Distance in Tree(点分治/树形dp)

    题意:求树上距离为k的点对个数: 解题关键:练习一下点分治不用容斥 而直接做的做法.注意先查询,后更新. 不过这个方法有个缺陷,每次以一个新节点为根,必须memset mp数组,或许使用map会好些, ...

  6. codeforces 161 D. Distance in Tree(树形dp)

    题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...

  7. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  8. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

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

随机推荐

  1. vue双向绑定原理源码解析

    当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理. 简易vue源码地址:https://github.com/maxlove123 ...

  2. Java线程总结(二)

    自定义线程的数据可以共享,也可以不共享,这要看具体的实现方式. 1.不共享数据多线程实现方式: public class MyThread extends Thread{ private int co ...

  3. LCD实验学习笔记(七):NAND FLASH

    s3c2440 CPU内置NAND FLASH控制器.相关寄存大器起始地址为0x4e000000. 通过设置NFCONF寄存器,设置NAND FLASH 时序. 通过设置NFCONT寄存器,使能NAN ...

  4. SUSE 11.3 linux ISO下载地址

    http://linux.iingen.unam.mx/pub/Linux/Suse/isos/SLES11/ SLE-11-SP3-SDK-DVD-i586-GM-DVD1.iso 6deaa960 ...

  5. 使用GDB命令行调试器调试C/C++程序【转】

    转自:https://linux.cn/article-4302-1.html 编译自:http://xmodulo.com/gdb-command-line-debugger.html作者: Adr ...

  6. C后端设计开发 - 第3章-气功-原子锁线程协程

    正文 第3章-气功-原子锁线程协程 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了. 童话镇 - http://music.163.com/#/m/song ...

  7. 设计模式之笔记--桥接模式(Bridge)

    桥接模式(Bridge) 定义 桥接模式(Bridge),将抽象部分与它的实现部分分离,使它们都可以独立地变化. 类图 描述 Abstraction:定义抽象部分的接口,通常在这个接口里面要维护一个实 ...

  8. django中使用第三方包实现定时任务

    # 转载请留言联系 在做主页静态化的时候,需要定时生成主页HTML,以保持数据的最新. 定时任务可以用第三方包django-crontab来实现. 附上官方文档:https://pypi.org/pr ...

  9. elasticsearch使用Analyze API

    curl -XGET 'localhost:9200/index_name/_analyze?pretty&field=type_name.field_name' -d 'Robots car ...

  10. linux命令(30):touch命令

    实例一:创建不存在的文件 touch test.log test1.log 实例二:更新log.log的时间和log2012.log时间戳相同 touch  -r test.log test1.log ...