【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整。
[L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少。
于是,我们现在只需要统计sigma((L+f(L,K))),最后除以K即可。
统计sigma(L)时,我们考虑计算每条边出现在了几条路径中,设u为edgei的子节点,那么这条边对答案的贡献就是siz(u)*(n-siz(u)),siz(u)为u的子树大小。
统计sigma(f(L,K))时,我们需要dp出f(u,r)表示从u的子树中出发,到u结束的路径中,长度mod K == r的有多少条。
具体统计方法跟
http://www.cnblogs.com/autsky-jadek/p/6580355.html
类似。
复杂度O(n*K^2)。
http://codeforces.com/blog/entry/51068
官方题解说得也很清楚。
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll ans;
int v[400010],first[200010],next[400010],e;
ll f[200010][5];
void AddEdge(int U,int V){
v[++e]=V;
next[e]=first[U];
first[U]=e;
}
int n,m,fa[200010],siz[200010];
void dfs(int U){
f[U][0]=1;
siz[U]=1;
int tot=0;
for(int i=first[U];i;i=next[i]){
if(!fa[v[i]]){
fa[v[i]]=U;
dfs(v[i]);
siz[U]+=siz[v[i]];
ans+=(ll)siz[v[i]]*(ll)(n-siz[v[i]]);
for(int j=0;j<m;++j){
for(int k=0;k<m;++k){
ans+=(f[U][j]*f[v[i]][k])*(ll)((j+k+1)%m==0 ? 0 : m-(j+k+1)%m);
}
}
for(int j=0;j<m;++j){
f[U][(j+1)%m]+=f[v[i]][j];
}
}
}
}
int main(){
// freopen("c.in","r",stdin);
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<n;++i){
scanf("%d%d",&x,&y);
AddEdge(x,y);
AddEdge(y,x);
}
fa[1]=-1;
dfs(1);
cout<<ans/(ll)m<<endl;
return 0;
}
【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps的更多相关文章
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E
Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
Description A tree is an undirected connected graph without cycles. The distance between two vertice ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路
A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
如果某个位置i是Y,直接直到i+m-1为止填上新的数字. 如果是N,直接把a[i+m-1]填和a[i]相同即可,这样不影响其他段的答案. 当然如果前面没有过Y的话,都填上0就行了. #include& ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C
Description In the army, it isn't easy to form a group of soldiers that will be effective on the bat ...
随机推荐
- Android Studio 中引入Library
启动AndroidStudio后,打开你需要接收Library的项目.比如有两个项目,项目A,和Library项目B,那么打开项目A.图中所示为项目的结构图,点击右上角的File菜单. 2 在下拉菜单 ...
- Java线程总结(一)
首先,先贴上一个简单的线程实例: public class MyThread extends Thread{ @Override public void run(){ try { for (int i ...
- Java多线程学习(二)synchronized关键字(2)
转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79670775 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...
- css 背景透明,文字不透明
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Python模块学习 - Functools
Functools模块 Higher-order functions and operations on callable objects,看这个标题我都是懵逼的,这都是啥啥啥啊,赶紧拿出百度翻译:可 ...
- java基础 运算符
算数运算符 加号:在操作数值.字符.字符串时其结果是不同的,当两个字符相加得到的是ASCII码表值, 当两个字符串相加时表示将两个字符串连接在一起,从而组成新的字符串. 除号:整数在使用除号操作时,得 ...
- linux网络编程之IO模型
本文转自作者:huangguisu 1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式:同步: 所谓 ...
- device tree --- label
[label:] <device node name>[@ unit-address] 為 device node 取 label name, 可以在其它位置使用 &label 存 ...
- Vue优化首屏加载
背景: 使用vue + iview搭建的一个后台管理系统,路由已经用了懒加载,加载登陆页面,居然还是需要18S左右,刚到一个新公司,项目经理很委婉的说,看看能不能优化了一下.然后就开始了网上一大堆'v ...
- vue验证码组件
1.效果图 2.全部代码: <template> <div class="join_formitem"> <label class="enq ...