Codeforces790B. Bear and Tree Jumps 【树型DP】
来源:互联网 发布:股票短线王软件 编辑:程序博客网 时间:2024/06/09 00:58
传送门
#include<stdio.h>#include<bits/stdc++.h>using namespace std;#define ll long long#define MEM(a,x) memset(a,x,sizeof(a))const int N = 2e5+5;vector<int>G[N];int sz[N],d[N][10];ll ans;int n,k;inline int mod(int x){ return (x%k+k)%k;}void init(int n){ for(int i=0;i<=n;++i){ G[i].clear(); sz[i]=0; MEM(d[i],0); } ans=0;}void dfs(int u,int far,int dept){ sz[u]=d[u][dept%k]=1; for(int v:G[u]){ if(v==far){ continue; } dfs(v,u,dept+1); for(int i=0;i<k;++i){ for(int j=0;j<k;++j){ int dis=mod(i+j-2*dept); ans+=(ll)mod(k-dis)*d[u][i]*d[v][j]; } } sz[u]+=sz[v]; for(int i=0;i<k;++i){ d[u][i]+=d[v][i]; } } ans+=(ll)sz[u]*(n-sz[u]);}int main(){ //freopen("/home/yang/Desktop/r.txt","r",stdin); while(~scanf("%d%d",&n,&k)){ init(n); for(int i=0;i<n-1;++i){ int u,v; scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } dfs(1,-1,0); printf("%lld\n",ans/k); } return 0;}
阅读全文
0 0
- Codeforces790B. Bear and Tree Jumps 【树型DP】
- Codeforces790B Bear and Tree Jumps -- 树形DP
- CF Bear and Tree Jumps树形DP
- CF791D-Bear and Tree Jumps
- codeforces 405 D. Bear and Tree Jumps 树形dp
- Codeforces 791D Bear And Tree Jumps 树形DP
- Codeforces Round #405 D. Bear and Tree Jumps 树形DP
- codeforces D. Bear and Tree Jumps
- codeforces 791D Bear and Tree Jumps
- 791D Bear and Tree Jumps
- Codeforces 711C. Bear and Tree Jumps【树形dp好题】
- CF771C:Bear and Tree Jumps(树形dp & 树上距离和)
- codeforces 711C. Bear and Tree Jumps(树上距离)
- Codeforces Round #405 Div. 1 B. Bear and Tree Jumps
- codeforces790B
- [BFS] CF653E. Bear and Forgotten Tree 2
- hdu5416 CRB and Tree(树型dp)
- Codeforces 653E:Bear and Forgotten Tree 2
- MySQL之进行行列转换
- Android TextView使用HTML处理字体样式、显示图片等
- LeetCode 191. Number of 1 Bits
- .net 实现http下载
- Python基础入门之List和Tuple类型三
- Codeforces790B. Bear and Tree Jumps 【树型DP】
- NSFZOJ #1063. 【NOIP2016】天天爱跑步
- 从字符串中提取出数字
- 项目阶段总结
- 并发编程修九:并发容器之CopyOnWriteArrayList
- Linux用户登录记录日志和相关查看命令汇总
- Spring框架之基础类—AttributeAccessorSupport抽象类
- 开发框架-Spring-错误或异常时返回统一的json格式
- HTML中的密码框