D. Bear and Tree Jumps
 

A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them.

Limak is a little polar bear. He lives in a tree that consists of n vertices, numbered 1 through n.

Limak recently learned how to jump. He can jump from a vertex to any vertex within distance at most k.

For a pair of vertices (s, t) we define f(s, t) as the minimum number of jumps Limak needs to get from s to t. Your task is to find the sum of f(s, t) over all pairs of vertices (s, t) such that s < t.

Input

The first line of the input contains two integers n and k (2 ≤ n ≤ 200 000, 1 ≤ k ≤ 5) — the number of vertices in the tree and the maximum allowed jump distance respectively.

The next n - 1 lines describe edges in the tree. The i-th of those lines contains two integers ai and bi (1 ≤ ai, bi ≤ n) — the indices on vertices connected with i-th edge.

It's guaranteed that the given edges form a tree.

Output

Print one integer, denoting the sum of f(s, t) over all pairs of vertices (s, t) such that s < t.

 
input
13 3
1 2
3 2
4 2
5 2
3 6
10 6
6 7
6 13
5 8
5 9
9 11
11 12
output
114
Note

In the first sample, the given tree has 6 vertices and it's displayed on the drawing below. Limak can jump to any vertex within distance at most 2. For example, from the vertex 5 he can jump to any of vertices: 1, 2 and 4 (well, he can also jump to the vertex 5 itself).

There are  pairs of vertices (s, t) such that s < t. For 5 of those pairs Limak would need two jumps: (1, 6), (3, 4), (3, 5), (3, 6), (5, 6). For other 10 pairs one jump is enough. So, the answer is 5·2 + 10·1 = 20.

In the third sample, Limak can jump between every two vertices directly. There are 3 pairs of vertices (s < t), so the answer is 3·1 = 3.

 题意:

  给你一棵树,求出所有f(S,T)的和;

  f(S,T)表示,ST在树上的距离除k取上整

题解

  k最大只有5

  设定DP[i][j]表示已i节点为根的子树上到i节点的距离%k=j的节点数,转移很简单

  fs[i][j]表示已i节点为根的子树上到i节点的距离%k=j的除k的大小

  细节需要处理好

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, M = 1e3+, mod = 1e9+, inf = 2e9; vector<int > G[N];
LL dp[N][],fs[N][];
LL ans = ;
int n,k,a,b;
void dfs(int u,int f) {
dp[u][] = ;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == f) continue;
dfs(to,u);
for(int j = ; j <= k; ++j) {
for(int h = ; h <= k; ++h) {
if(!dp[u][j]||!dp[to][h]) continue;
LL tmp = j+h+;
if(tmp%k) tmp = tmp/k+;
else tmp = tmp/k;
ans += tmp*dp[to][h]*dp[u][j]+fs[u][j]*dp[to][h]+fs[to][h]*dp[u][j];
}
}
for(int j = ; j <= k; ++j)
dp[u][j] += dp[to][j-],fs[u][j] += fs[to][j-];
dp[u][] += dp[to][k];fs[u][] += fs[to][k]+dp[to][k];
}
}
int main() {
scanf("%d%d",&n,&k);
for(int i = ; i < n; ++i) {
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs(,);
cout<<ans<<endl;
return ;
}

Codefoces 791D. Bear and Tree Jumps 树形DP的更多相关文章

  1. CodeForces 771C Bear and Tree Jumps 树形DP

    题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...

  2. Codeforces 791D Bear and Tree Jump(树形DP)

    题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...

  3. [CF791D]Bear and Tree Jumps

    题目描述 A tree is an undirected connected graph without cycles. The distance between two vertices is th ...

  4. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  5. BZOJ-3227 红黑树(tree) 树形DP

    个人认为比较好的(高端)树形DP,也有可能是人傻 3227: [Sdoi2008]红黑树(tree) Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1 ...

  6. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  7. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. URAL1018 Binary Apple Tree(树形DP)

    题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...

  9. ural 1018 Binary Apple Tree(树形dp | 经典)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

随机推荐

  1. laravel 集成 swagger插件

    原文链接:https://medium.com/@mahbubkabir/discovering-swagger-in-laravel-rest-apis-cb0271c8f2 1.composer ...

  2. LeetCode02-两数相加

    ''' 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示 ...

  3. python操作剪贴板错误提示:pywintypes.error: (1418, 'GetClipboardData',线程没有打开的剪贴板)

    问题现象:通过打断点,一步步调试可以正常复制和粘贴剪贴板数据.但是直接运行会报错pywintypes.error: (1418, 'GetClipboardData',线程没有打开的剪贴板) 问题原因 ...

  4. jenkins在linux环境搭建需要用到的linux命令

    需要用到的linux命令如下: 服务器jdk1.7/usr/java/jdk1.7.0_80 jdk1.8/home/hujb/javaJDK/jdk1.8.0_171保存文件时用 : w ! sud ...

  5. CDOJ 1218 Pick The Sticks

    Pick The Sticks Time Limit: 15000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others ...

  6. 70.打印所有Spring boot载入的bean【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 问题的提出: 我们在开发过程当中,我们可能会碰到这样的问题:No qualifying bean  就是我们定义的bean无法进行注入,那到底是什 ...

  7. NYOJ-613//HDU-1176-免费馅饼,数字三角形的兄弟~~

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人 ...

  8. python中的“坑”—持续更新

    1.判断是否是回文 def is_back(s): ]==(s if s.strip() else False) print(is_back('上海自来水来自海上')) print(is_back(' ...

  9. [luoguP2982][USACO10FEB]慢下来Slowing down(dfs序 + 线段树)

    传送门 这个题显然可以用树链剖分做. 然而线段树也能做. 每个点都对它的子树有贡献,所以先求一边 dfs序,然后直接在 dfs序 中搞 线段树 就行. ——代码 #include <cstdio ...

  10. POJ2014 Flow Layout

      Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3161   Accepted: 2199 Description A f ...