Codeforces 711C. Bear and Tree Jumps【树形dp好题】
来源:互联网 发布:排序算法中空间复杂度 编辑:程序博客网 时间:2024/05/29 09:47
题目:http://codeforces.com/contest/791/problem/D
题意:
你可以从树上的节点一次最多走k条边。
(称为跳一次);
树为无权树;
然后问你任意两点之间的条的次数的和为多少;
分析:
不会做,看题解:http://codeforces.com/blog/entry/51068
代码:
#include <bits/stdc++.h>using namespace std;const int nax = 2e5 + 5;vector<int> edges[nax];int count_subtree[nax][5];int total_subtree[nax];long long answer;int n, k;int subtract(int a, int b) { return ((a - b) % k + k) % k;}void dfs(int a, int par, int depth) { count_subtree[a][depth % k] = total_subtree[a] = 1; for(int b : edges[a]) if(b != par) { dfs(b, a, depth + 1); for(int i = 0; i < k; ++i) for(int j = 0; j < k; ++j) { // compute distance modulo k int remainder = subtract(i + j, 2 * depth); // compute x such that (distance + x) is divisible by k int needs = subtract(k, remainder); answer += (long long) needs * count_subtree[a][i] * count_subtree[b][j]; } for(int i = 0; i < k; ++i) count_subtree[a][i] += count_subtree[b][i]; total_subtree[a] += total_subtree[b]; } // in how many pairs we will count the edge from 'a' to its parent? answer += (long long) total_subtree[a] * (n - total_subtree[a]);}int main() { scanf("%d%d", &n, &k); for(int i = 0; i < n - 1; ++i) { int a, b; scanf("%d%d", &a, &b); edges[a].push_back(b); edges[b].push_back(a); } dfs(1, -1, 0); assert(answer % k == 0); printf("%lld\n", answer / k);}
0 0
- Codeforces 711C. Bear and Tree Jumps【树形dp好题】
- codeforces 405 D. Bear and Tree Jumps 树形dp
- Codeforces 791D Bear And Tree Jumps 树形DP
- Codeforces Round #405 D. Bear and Tree Jumps 树形DP
- CF Bear and Tree Jumps树形DP
- Codeforces790B Bear and Tree Jumps -- 树形DP
- codeforces 711C. Bear and Tree Jumps(树上距离)
- codeforces D. Bear and Tree Jumps
- codeforces 791D Bear and Tree Jumps
- Codeforces790B. Bear and Tree Jumps 【树型DP】
- CF771C:Bear and Tree Jumps(树形dp & 树上距离和)
- Codeforces Round #405 Div. 1 B. Bear and Tree Jumps
- CF791D-Bear and Tree Jumps
- 791D Bear and Tree Jumps
- Codeforces--658C--Bear and Forgotten Tree 3(模拟&&技巧)(好题)
- 【Codeforces 736C】 Ostap and Tree【树形DP】
- Codeforces 658C Bear and Forgotten Tree 3【构造】
- CodeForces 658C Bear and Forgotten Tree 3(构造)
- SQL排序后将序号填入指定字段
- 百度首席科学家吴恩达宣布将从百度离职
- 如何将24位RGB颜色转换16位RGB颜色
- linux用tar 解压tar文件报错
- 编辑距离(同源去重算法)
- Codeforces 711C. Bear and Tree Jumps【树形dp好题】
- 斯坦纳树
- HDU3466 Proud Merchants(贪心+01背包)
- explicit关键字的作用
- 【Android】win10下Android Studio写中文注释的时候候选窗不跟随光标(在右下角)
- spark中的hash shuffle
- [iBET Malaysia]Sports Book Rebate 0.35% Bonus
- 微博抓取之各种方法收集
- Swift实现最简单的UICollectionView