CodeForces 771C Bear and Tree Jumps 树形DP
题意:
给出一棵树,一个人可以在树上跳,每次最多跳\(k(1 \leq k \leq 5)\)个点
定义\(f(s,t)\)为从顶点\(s\)跳到顶点\(t\)最少需要跳多少次
求\(\sum\limits_{s<t}f(s,t)\)
分析:
注意到\(k\)很小,为了方便转移,定义:
- \(sz(u,i)\)为\(u\)的子树中与\(u\)的距离模\(k\)等于\(i\)的孩子节点的个数
- \(d(u,i)\)为从\(u\)跳到\(sz(u,i)\)对应的节点的跳数之和
转移:
- \(sz(u,(i+1)\, mod \, k) += sz(v, i)\),其中\(v\)是\(u\)的儿子节点
- \(d(u,(i+1)\, mod \, k) += d(v, i), 0<i<k\)
- \(d(u,1 \, mod \, k) += d(v,0) + sz(v, 0)\),因为到\(v\)距离为\(k\)的整数被的那些节点转移到\(u\)时会多跳一步
统计答案:
在递归到点\(u\)时,我们统计那些\(lca(s,t)=u\)的路径的贡献
这样的路径有两类:
- \(u\)到其孩子节点的路径
- 来自\(u\)的两个不同子树的孩子节点形成的路径
第一类答案很好计算,就是\(\sum d(u,i)\)
在第二类答案时,假设枚举到\(u\)的某个子树\(v\),而且之前的子树的答案已经更新到到\(d(u,i),sz(u,i)\)里面
枚举\(i,j\)分别为\(v\)之前的子树距离\(u\)模\(k\)为\(i\)的路径,和子树\(v\)中距离\(u\)模\(k\)为\(j\)的路径
两边各选一段路拼起来,可以通过公式快速求得答案,细节不再赘述
最终时间复杂度为\(O(k^2n)\)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 200000 + 10;
vector<int> G[maxn];
int n, k, sz[maxn][5];
LL d[maxn][5], ans;
void dfs(int u, int fa) {
int tsz[5];
LL td[5];
for(int v : G[u]) if(v != fa) {
dfs(v, u);
memset(tsz, 0, sizeof(tsz));
for(int i = 0; i < k; i++) tsz[(i+1)%k] += sz[v][i];
memset(td, 0, sizeof(td));
for(int i = 1; i < k; i++) td[(i+1)%k] += d[v][i];
td[1%k] += d[v][0] + sz[v][0];
for(int i = 0; i < k; i++) {
for(int j = 0; j < k; j++) {
int delta = (i+j+k-1)/k - (i>0) - (j>0);
ans += d[u][i] * tsz[j] + td[j] * sz[u][i];
if(delta) ans += (LL) delta * sz[u][i] * tsz[j];
}
}
for(int i = 0; i < k; i++) { d[u][i] += td[i]; sz[u][i] += tsz[i]; }
}
for(int i = 0; i < k; i++) ans += d[u][i];
sz[u][0]++;
}
int main()
{
scanf("%d%d", &n, &k);
for(int i = 1; i < n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1, 0);
printf("%lld\n", ans);
return 0;
}
CodeForces 771C Bear and Tree Jumps 树形DP的更多相关文章
- Codefoces 791D. Bear and Tree Jumps 树形DP
D. Bear and Tree Jumps A tree is an undirected connected graph without cycles. The distance betwee ...
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- CodeForces 161D Distance in Tree【树形DP】
<题目链接> 题目大意:一颗无向无环树,有n个顶点,求其中距离为k的点对数是多少,(u,v)与(v,u)为同一点对. #include <cstdio> #include &l ...
- Codeforces 791D Bear and Tree Jump(树形DP)
题目链接 Bear and Tree Jumps 考虑树形DP.$c(i, j)$表示$i$最少加上多少后能被$j$整除. 在这里我们要算出所有$c(i, k)$的和. 其中$i$代表每个点对的距离, ...
- 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 ...
- Codeforces 804D Expected diameter of a tree(树形DP+期望)
[题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...
- codeforces 161 D. Distance in Tree(树形dp)
题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设 ...
- Codeforces Round #551 (Div. 2) D. Serval and Rooted Tree (树形dp)
题目链接 题意:给你一个有根树,假设有k个叶子节点,你可以给每个叶子节点编个号,要求编号不重复且在1-k以内.然后根据节点的max,minmax,minmax,min信息更新节点的值,要求根节点的值最 ...
- [CF791D]Bear and Tree Jumps
题目描述 A tree is an undirected connected graph without cycles. The distance between two vertices is th ...
随机推荐
- python+pywinauto之PC端自动化一
所需软件安装: 1.下载 pywinauto 安装参考: https://jingyan.baidu.com/article/414eccf6a1a3906b421f0a59.html 下载地址: h ...
- 微信小程序加载本地图片方法
目录结构如下,只要图片按正确的方式放入小程序的开发工具的项目中,即可在wxml文件中用内联样式或者image标签都可以引用本地的图片. 步骤一:微信开发工具 打开项目 步骤二:新建个文件夹(放项目的一 ...
- matlab中padarray函数在numpy、python中的实现
a = np.arange(6) a = a.reshape((2, 3)) print np.lib.pad(a, 1, 'symmetric') 运行结果: [[ ] [ ] [ ] [ ]]
- 微信H5单页面滑动的时候如何避免出界,出现头部和底部的黑底?
ios系统微信浏览器.safari浏览器中h5页面上拉下滑导致悬浮层脱离窗口的解决方法 ios偶现下拉出现黑底时,界面第一次上拉时拉不动的解决方案: document.querySelector('# ...
- 基于java开发的开源代码GPS北斗位置服务监控平台
最近在研究位置服务平台,基于全球卫星定位技术(GNSS).互联网技术.空间地理信息技术(GIS).3G/4G无线通信技术,面向全国公众用户建立大容量.实时.稳定的位置信息服务运营平台.实现管理目标的实 ...
- SpringBoot学习12:springboot异常处理方式2(使用@ExceptionHandle注解)
1.编写controller package com.bjsxt.controller; import org.springframework.stereotype.Controller; impor ...
- noip2018 洛谷 P5020 货币系统
关键: 要使m最小,(m,b)中的数不能用(n,a)中的数表示出来 对于 3 19 10 6 19=10+3+3+3 6=3+3 只有3 和 10 不能被(n,a)中的数表示 所以m=2 只需要 ...
- lintcode_397_最长上升连续子序列
最长上升连续子序列 描述 笔记 数据 评测 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到 ...
- hdu 2828 Buy Tickets
Buy Tickets Time Limit : 8000/4000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- IO流的应用_Copy文件
IO流的应用_Copy文件 (1) import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...