codeforces 791D Bear and Tree Jumps


D. Bear and Tree Jumps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 0001 ≤ 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 most 2. For example, from the vertex 5 he can jump to any of vertices: 12 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条边,问树上两两点间的距离和为多少。

思路:讲真,这玩意我看了题解也看了一下午才懂。。智商太低。题解的思路是这样的,首先当k=1时,算所有边经过的次数和就是答案。这个应该很容易懂吧不解释了。计算方法也很简单,基础树形dp而已。麻烦的来了。k>1时,计算方法是这样的,例如两点之间隔了10条边,你每次能跳3条边,那么当你跳了3次之后就跳了9条边,剩下一次虽然你能跳3条边,但是也只差一条边的距离,从此我们能想到把缺的边数补上2,用12/3就知道这两点的距离是多少了。也就是说,我们接下来需要计算的是两两点之间还差多少步能被k整除,然后累加起来,把结果/k就是答案了。然后问题就转移到了怎么求两两之间的步长,我们跑dfs的时候记录每个点的深度,然后两重循环枚举深度,任意两点步长等于depth[x]+depth[y]-depth[z]*2,z为两点最近的公共祖先的深度,那么这道题就能解出来了。下面给代码:

#include<iostream>  
#include<cmath>  
#include<queue>  
#include<cstdio>  
#include<queue>  
#include<algorithm>  
#include<cstring>  
#include<string>  
#include<utility>
#include<map>
#include<vector>
#define maxn 200005
#define inf 0x3f3f3f3f  
using namespace std;
typedef long long LL;
const double eps = 1e-8;
struct node{
	int v, next;
}p[maxn << 1];
int head[maxn], n, k;
LL dp[maxn][10], size[maxn], ans;
void dfs(int x, int fa,int depth){
	size[x] = dp[x][depth%k] = 1;
	for (int i = head[x]; ~i; i = p[i].next){
		if (p[i].v == fa)
			continue;
		dfs(p[i].v, x, depth + 1);
		for (int a = 0; a < k; a++){
			for (int b = 0; b < k; b++){
				int dis = ((a + b) % k - (depth * 2) % k + k) % k;
				ans += ((k - dis) % k)*dp[x][a] * dp[p[i].v][b];
			}
		}
		for (int a = 0; a < k; a++){
			dp[x][a] += dp[p[i].v][a];
		}
		ans += size[p[i].v] * (n - size[p[i].v]);
		size[x] += size[p[i].v];
	}
}
int main(){
	scanf("%d%d", &n, &k);
	memset(head, -1, sizeof(head));
	int len = 0;
	for (int i = 0; i < n - 1; i++){
		int u, v;
		scanf("%d%d", &u, &v);
		p[len].v = v;
		p[len].next = head[u];
		head[u] = len++;
		p[len].v = u;
		p[len].next = head[v];
		head[v] = len++;
	}
	dfs(1, 0, 0);
	printf("%lld\n", ans / k);
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值