题目链接 Distance in Tree

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

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

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

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

 #include <bits/stdc++.h>

 using namespace std;

 int f[][], deep[];
vector <int> v[];
int n, k, x, y;
long long ans; void dfs(int x, int fa, int dep){
int c[];
deep[x] = dep;
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, dep + );
memset(c, , sizeof c); c[] = ;
for (int i = ; i <= k; ++i) c[i + ] = f[u][i];
for (int i = ; i <= k - ; ++i) ans += (long long)f[x][i] * c[k - i];
for (int i = ; i <= k; ++i) f[x][i] += c[i];
}
} int main(){ ans = ;
scanf("%d%d", &n, &k);
for (int i = ; i <= n - ; ++i){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} memset(deep, , sizeof deep);
dfs(, , );
for (int i = ; i <= n; ++i) if (deep[i] >= k) ++ans;
printf("%lld\n", ans); return ; }

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. DFS:Prime Ring Problem(素数环)

    解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...

  2. 2940: [Poi2000]条纹(Multi_SG)

    2940: [Poi2000]条纹 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 114  Solved: 72[Submit][Status][Dis ...

  3. loj2090 「ZJOI2016」旅行者

    分治+最短路,很套路的 #include <algorithm> #include <iostream> #include <cstring> #include & ...

  4. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  5. 【Remove Duplicates from Sorted Array II】cpp

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  6. leetcode 【 Remove Duplicates from Sorted Array 】python 实现

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  7. Web 安全问题 rel="noopener nofollw"

    1. noopener 如果你需要用 a 标签打开一个标签页时,你会使用 target='_blank' 这个属性,此时你需要添加 rel='noreferrer noopener' 当你使用 tar ...

  8. Leetcode 493.翻转对

    翻转对 给定一个数组 nums ,如果 i < j 且 nums[i] > 2*nums[j] 我们就将 (i, j) 称作一个重要翻转对. 你需要返回给定数组中的重要翻转对的数量. 示例 ...

  9. [oldboy-django][2深入python] orm中auto_now =True, antu_now_add=True的应用

    DateTimeField.auto_now 这个参数的默认值为false,设置为true时,能够在保存该字段时,将其值设置为当前时间,并且每次修改model,都会自动更新.因此这个参数在需要存储“最 ...

  10. DFS和BFS遍历的问题

    来自https://github.com/soulmachine/leetcode 广度优先搜索 输入数据:没有什么特征,不像dfs需要有递归的性质.如果是树/图,概率更大. 状态转换图:数或者DAG ...