Description

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

Examples
input
6 2
1 2
1 3
2 4
2 5
4 6
output
20
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
input
3 5
2 1
3 1
output
3
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 most2. 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.

题意:给出一棵树,和最多跳K个数字,f(s,t)表示从s到t需要跳的最少次数,问那么一棵树每两个点跳的次数之和是多少?

解法:如果只跳一次,那每个点经历的次数为这个点的子树节点个数*(n-这个点的子树节点个数),那么K>=2的情况,对于任意两个点的x->y 距离为 深度[x]+深度[y]-2*最近公共祖先深度[z]

dp[v][d%k]表示v为节点开始,深度为d%k的个数,比如2为节点开始,深度为1的有4和5

4在2的子树内,4开始深度为1点为6,我们更新到dp[2][1]内,就是dp[2][1]+=dp[4][1]

sum[v]表示v的子树节点个数

最后答案为ans/n,比如距离为5,最多跳3步,其实是跳(5+1)/3=2次就好了,注释也有解释

(题解好难懂啊,我也不知道说清楚了没)

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
ll dp[maxn][],sum[maxn];
vector<int>q[maxn];
ll vis[maxn];
ll cnt;
ll n,k;
void dfs(int v,int d,int fa)
{
dp[v][d%k]=sum[v]=;
//当前子节点就v一个
for(int i=;i<q[v].size();i++)
{
// cout<<v<<endl;
int pos=q[v][i];
if(pos==fa) continue;
dfs(pos,d+,v);
for(int x=;x<k;x++)
{
for(int y=;y<k;y++)
{
int ans=((x+y)%k-(d*)%k+k)%k;
//ans为缺少部分,比如5跳3,少了两步
cnt+=((k-ans)%k)*dp[v][x]*dp[pos][y];
//少了两步,为了达成三步,必须多走一步,所以为k-ans,每个点多走k-ans步,相乘
}
}
for(int x=;x<k;x++)
{
dp[v][x]+=dp[pos][x];
//子节点记录部分更新到父结点
}
cnt+=sum[pos]*(n-sum[pos]);
//讨论k=1的情况,种数==为pos节点包含子节点*以外的节点
sum[v]+=sum[pos];
//将pos包含节点个数更新到父结点 }
}
int main()
{
cin>>n>>k;
for(int i=;i<n-;i++)
{
int u,v;
cin>>u>>v;
q[u].push_back(v);
q[v].push_back(u);
}
dfs(,,);
cout<<cnt/k<<endl;
return ;
}

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D的更多相关文章

  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. 【树形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的整数倍,还要加上多少. 于是, ...

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

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

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

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

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

  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. C/C++用状态转移表联合函数指针数组实现状态机FSM

    状态机在project中使用很的频繁,有例如以下常见的三种实现方法: 1. switch-case 实现.适合简单的状态机. 2. 二维状态表state-event实现.逻辑清晰.可是矩阵通常比較稀疏 ...

  2. field 属性操作

    首先必须明一点 Field类主要是用来辅助获取和操作类的属性的! 1.怎么通过反射获取类的属性 先来看JDK提供的方法有如下几种: a)Class.getDeclaredField(String na ...

  3. Java异步套接字实例

    服务端 package com.test.server; import java.io.IOException; import java.net.InetSocketAddress; import j ...

  4. YTU 2577: 小数计算——结构体

    2577: 小数计算--结构体 时间限制: 1 Sec  内存限制: 128 MB 提交: 978  解决: 647 题目描述 小数可以看成是一个点和两个数组成的,因此可以定义成一个小数的结构体,现在 ...

  5. iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用

    目的 本文主要是分享iOS多线程的相关内容,为了更系统的讲解,将分为以下7个方面来展开描述. 多线程的基本概念 线程的状态与生命周期 多线程的四种解决方案:pthread,NSThread,GCD,N ...

  6. codeforces 665E E. Beautiful Subarrays(trie树)

    题目链接: E. Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input ...

  7. 棋盘问题(dp)

    棋盘问题 传送门 题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个 ...

  8. Linux终端那件事儿

    我们将会讨论如何更好的控制用户终端:也就说是键盘输入与屏幕输出.除了这些,我们还会了解我们编写的程序如何由用户处读取输入,即使是在输入重定向的情况下,以及确保输出到屏幕的正确位置.这里所提供的一些底层 ...

  9. relative和absolute

    relative 相对定位 1. 幻影瞬移 absolute属性也有瞬移技能,不同之处在于:absolute属性以天空或其他外界限制计算瞬移位置:而relative属性由于是凡人肉体,偏移能力有限,只 ...

  10. bzoj4556

    后缀自动机+二分+倍增+线段树合并 后缀自动机真好用 后面一个串是固定的,那么我们要对前面的串进行一些操作.我们想既然是求lcp,那么我们得先翻转原串,这样前缀变成了后缀,然后二分一下,从d在自动机上 ...