Codefoces 791D. Bear and Tree Jumps 树形DP
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.
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.
Print one integer, denoting the sum of f(s, t) over all pairs of vertices (s, t) such that s < t.
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
114
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的更多相关文章
- CodeForces 771C Bear and Tree Jumps 树形DP
题意: 给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点 定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次 求\(\sum\lim ...
- Codeforces 791D Bear and Tree Jump(树形DP)
题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...
- [CF791D]Bear and Tree Jumps
题目描述 A tree is an undirected connected graph without cycles. The distance between two vertices is th ...
- 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 ...
- BZOJ-3227 红黑树(tree) 树形DP
个人认为比较好的(高端)树形DP,也有可能是人傻 3227: [Sdoi2008]红黑树(tree) Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1 ...
- 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 ...
- 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 ...
- URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...
- ural 1018 Binary Apple Tree(树形dp | 经典)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
随机推荐
- JavaScript基础对象---Map
一.创建Map对象 Map 对象保存键值对.任何值(对象或者原始值) 都可以作为一个键或一个值 1.构造函数 语法:new Map([iterable])参数: iterable 可 ...
- 又见GCD (已知最大公约数和其中一个数求另一个数)
#include<cstdio> int f1(int a,int b) //最大公约数 { ) return b; else return f1(b,a%b); } int f2(int ...
- python常用函数 C
1. Counter(hashable) 直接使用统计可哈希元素每个元素的数量. 2. most_common:可以统计数量最多的n个元素. from collections import Count ...
- ES6(Proxy 和 Reflect)
Proxy 和 Reflect 1.Proxy 和 Reflect 的概念 Proxy 意为 ‘代理’,连接了用户和真实对象之间的一个层 Reflect 意为‘反射’ 反射的是Object 2.适 ...
- 活动预告丨易盾CTO朱浩齐将出席2018 AIIA大会,分享《人工智能在内容安全的应用实践》
本文来自网易云社区 对于很多人来讲,仿佛昨天才燃起来的人工智能之火,转眼间烧遍了各个角落,如今我们的生活中,处处渗透着人工智能.10月16日,2018年 AIIA人工智能开发者大会在苏州举办,网易云易 ...
- 基于神经网络的embeddding来构建推荐系统
在之前的博客中,我主要介绍了embedding用于处理类别特征的应用,其实,在学术界和工业界上,embedding的应用还有很多,比如在推荐系统中的应用.本篇博客就介绍了如何利用embedding来构 ...
- [转]SQL SERVER数据库还原的方法
SQL SERVER数据库还原的方法 在SQL SERVER 2005下还原数据库 1.新建数据库A,右键还原数据库,此时目标数据库为A,选择备份 文件B_db_201311040200.BAK,还原 ...
- 只有代码不会撒谎,如何通过Spring boot源码查看其对于各个框架的默认配置
我发现很多开发对于看源码都有种恐惧心理,其实不必这样,大部分优秀的源码写的都挺直观的,很多时候,你在搜索引擎上搜到的一些东西并不一定是对的,但源码肯定造不了假,毕竟不管你怎么想,它就在那里,该是什么意 ...
- 【UOJ179】线性规划(单纯形)
题意: 思路:单纯形模板 ..,..]of double; idx,idy,q:..]of longint; c:..]of double; n,m,i,j,op,x,y:longint; eps,m ...
- HDU1533 最小费用最大流
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...