codeforces 711C. Bear and Tree Jumps(树上距离)
2017-03-19 23:58
381 查看
题目链接
C. Bear and Tree Jumps分析
其实题解已经说的很清楚了,我最开始不理解的一点在于如何计算剩余f(L,k),看了题解的代码之后才懂了,用一个数组cnt_sub[v][i] 存储以 v 为根的与根节点距离的剩余为 i 的节点数目.然后对于子树 u中的顶点 a,b 计算他们距离的剩余的时候,应为这两个节点u,v的距离一定是 du+dv−2∗(LCA的深度) (由于是对它的直接后继进行计算,所以子树深度就是LCA的深度). 由于MOD的性质他们距离的剩余就可以直接枚举k2种情况,然后减去子树深度来获得距离的剩余,以后的事情你一定懂了,就直接递归下去就行了.
题解搬运工
AC code
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 2e5+10; std::vector<int> G[maxn]; int tatol_sub[maxn]; int cnt_sub[maxn][5];//子树中每个剩余的点的总数 LL ans; LL n,k; int sub_mod(int a,int b){ return ((a-b)%k+k)%k; } void dfs(int u,int p,int dep){ cnt_sub[u][dep%k] = tatol_sub[u] = 1; for(int v: G[u]){ if(v!=p){ dfs(v,u,dep+1); for(int i=0 ; i<k ; ++i){ for(int j =0 ; j<k ; ++j){ int remain = sub_mod(i+j,dep*2);//当前子树中两个节点的距离 int need = sub_mod(k,remain); ans+=(LL)need*cnt_sub[u][i]*cnt_sub[v][j]; } } for(int i = 0 ; i<k ; ++i) cnt_sub[u][i]+=cnt_sub[v][i]; tatol_sub[u]+=tatol_sub[v]; } } ans+=(n-tatol_sub[u])*tatol_sub[u]; } int main(int argc, char const *argv[]) { cin>>n>>k; ans = 0; for(int i=0 ; i<n-1 ; ++i){ int u,v; cin>>u>>v; G[u].push_back(v); G[v].push_back(u); } dfs(1,-1,0); std::cout << ans/k << '\n'; return 0; }
相关文章推荐
- Codeforces 711C. Bear and Tree Jumps【树形dp好题】
- CF771C:Bear and Tree Jumps(树形dp & 树上距离和)
- CodeForces 771C Bear and Tree Jumps 树形DP
- 【codeforces 791D】 Bear and Tree Jumps
- Codeforces 771C:Bear and Tree Jumps
- codeforces 405 D. Bear and Tree Jumps 树形dp
- CodeForces 639 B.Bear and Forgotten Tree 3(构造)
- Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】
- Codeforces--658C--Bear and Forgotten Tree 3(模拟&&技巧)(好题)
- CodeForces - 842C Ilya And The Tree(树上倍增)
- Codeforces Round #405 Div. 1 B. Bear and Tree Jumps
- CodeForces 682C Alyona and the Tree (树上DFS)
- CF791D-Bear and Tree Jumps
- codeforces 812 E. Sagheer and Apple Tree(树上博弈)
- CodeForces - 842C Ilya And The Tree 树上DFS
- codeforces D. Bear and Tree Jumps
- codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)
- CF Bear and Tree Jumps树形DP
- [树上启发式合并 && 哈希] Codeforces 741D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
- Codeforces 842C - Ilya And The Tree 【树上DFS】