题目链接 Distance in Tree

$k <= 500$ 这个条件十分重要。

设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数。

状态转移的时候,一个结点的信息由他的儿子转移过来。

那么一边进行状态转移,一边统计答案即可。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int f[][], deep[];
  6. vector <int> v[];
  7. int n, k, x, y;
  8. long long ans;
  9.  
  10. void dfs(int x, int fa, int dep){
  11. int c[];
  12. deep[x] = dep;
  13. for (auto u : v[x]){
  14. if (u == fa) continue;
  15. dfs(u, x, dep + );
  16. memset(c, , sizeof c); c[] = ;
  17. for (int i = ; i <= k; ++i) c[i + ] = f[u][i];
  18. for (int i = ; i <= k - ; ++i) ans += (long long)f[x][i] * c[k - i];
  19. for (int i = ; i <= k; ++i) f[x][i] += c[i];
  20. }
  21. }
  22.  
  23. int main(){
  24.  
  25. ans = ;
  26. scanf("%d%d", &n, &k);
  27. for (int i = ; i <= n - ; ++i){
  28. scanf("%d%d", &x, &y);
  29. v[x].push_back(y);
  30. v[y].push_back(x);
  31. }
  32.  
  33. memset(deep, , sizeof deep);
  34. dfs(, , );
  35. for (int i = ; i <= n; ++i) if (deep[i] >= k) ++ans;
  36. printf("%lld\n", ans);
  37.  
  38. return ;
  39.  
  40. }

Codeforces 161D Distance in Tree(树型DP)的更多相关文章

  1. CodeForces 160D - Distance in Tree 树型DP

    题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...

  2. CodeForces 161D Distance in Tree【树形DP】

    <题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...

  3. D. Distance in Tree(树型Dp计数)

    \(其实思路都能想到一点,就是去重这里特别麻烦,没有好的思路.\) \(设dp[i][j]为以i为根深度为j的节点数量\) \(dp[parent][j]=\sum{dp[son][j-1]}\) \ ...

  4. Codeforces 149D Coloring Brackets(树型DP)

    题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...

  5. codeforces 161D Distance in Tree 树形dp

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

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

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

  7. Educational Codeforces Round 52 (Rated for Div. 2) F. Up and Down the Tree 树型DP

    题面 题意:给你一棵树,你起点在1,1也是根节点,你每次可以选择去你子树的某个叶子节点,也可以选择,从叶子节点返回距离不超过k的一个根, 也就是说,你从1开始,向下跳,选择一个叶子(就是没有子树的节点 ...

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

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

  9. POJ 2486 Apple Tree ( 树型DP )

    #include <iostream> #include <cstring> #include <deque> using namespace std; #defi ...

随机推荐

  1. (ADO.NET)SqlCommand参数化查询

    string strcon = "Persist Security Info=False;User id=sa;pwd=lovemary;database=student;server=(l ...

  2. hibernate实体xml一对多关系映射

    单向一对多关系映射: 一个房间对应多个使用者,也就是Room實例知道User實例的存在,而User實例則沒有意識到Room實例. 用户表: package onlyfun.caterpillar; p ...

  3. [项目2] 10mins

    1.准备工作 M层:生成虚假数据 from django.db import models from faker import Factory # Create your models here. c ...

  4. C++ map 的用法归纳2

    [尊重原著: http://blog.csdn.net/zcf1002797280/article/details/7847819] Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序 ...

  5. c语言入门-01

    当我们学c语言我们学些什么. [1]编译机制 当我们写好c的代码,生产了程序,这中间到底做了些什么? 这个就是c语言的编译过程 我们分别来解析这上面的过程. 我们写出我们第一个c程序. #includ ...

  6. CSU-2019 Fleecing the Raffle

    CSU-2019 Fleecing the Raffle Description A tremendously exciting raffle is being held, with some tre ...

  7. 文本处理grep命令

    this is a words file. words words to be , , , , , , , , , , beginning linux programming 4th edition ...

  8. [译]为什么pandas有些命令用括号结尾,有些则没有?

    文章来源:https://nbviewer.jupyter.org/github/justmarkham/pandas-videos/blob/master/pandas.ipynb 方法:用括号调用 ...

  9. mvc项目限制请求类型

    MVC 5限制所有HTTP请求必须是POST方式 这篇文章讲述了如何限制请求类型.

  10. 节点流——FileInputStream&FileOutputStream

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...