我们要统计的答案是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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)

    A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【构造】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& ...

  9. 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 ...

随机推荐

  1. 比 file_get_contents() 更优的 cURL 详解(附实例)

    PHP 可以使用 file_get_content() 函数抓取网页内容,但却无法进行更复杂的处理,譬如文件的上传或下载. Cookie 操作等等.而 cURL 提供了这些功能. 一.cURL简介 在 ...

  2. ADC 計算時,階數的選擇

    reference : ADC 階數的計算

  3. 64_g3

    gimp-resynthesizer-2.0-6.20160601git787ee5a.fc2..> 11-Feb-2017 05:36 77650 gimp-save-for-web-0.29 ...

  4. Centos_Lvm expand capacity without restarting CentOS

    Rescan the new disk(/dev/sdb): #ls /sys/class/scsi_host/ host0 host1 host2 [root@db210_13:56:14 /dat ...

  5. hadoop中setOutputKeyClass和setOutputValueClass里类型

    初学mapreduce programing,纠结一天的问题如下: job.setOutputKeyClass和job.setOutputValueClas在默认情况下是同时设置map阶段和reduc ...

  6. spring boot&&cloud干货系列

    接触spring boot也有些时日了,刚开始博主还想参照官方参考指南自己写一个系列的入门式的教程,包含spring boot的所有模块的用法,后来发现,有一大波优秀的系列文章和项目了,所以就没班门弄 ...

  7. DataSet、DataTable、DataRow的数据复制方法

    DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...

  8. Nginx配置问题总结

    1.Nginx直接下载解压,有个nginx.exe文件,双击即开启Nginx服务(windows系统下).默认是80端口. 若服务无法启动,考虑以下三方面问题: (1)端口号80是否被占用 (2)防火 ...

  9. 使用python读取文本中结构化数据

    需求 read some .txt file in dir and find min and max num in file. solution: echo *.txt > file.name ...

  10. matlab实用命令

    实用命令 打点测时 在需要测量的开始部分标记: tic 在需要测量的结束部分标记: toc 记录程序从tic到toc运行所花费的时间 Image 翻转 fliplr(x) //左右翻转 flipud( ...