bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4543
题解
这道题的弱化版 bzoj3522 [POI2014]Hotel 的做法有好几种吧。
我一开始是另一种做法,所以别人说这个题目可以有长链剖分来加速的时候怎么也想不出来。
枚举 \(i\),令点 \(i\) 为根,统计三个人的中心是 \(i\) 的情况。首先三个人一定在不同的子树中,然后分层统计一下就好了。
还有一个种纯 dp 的做法:
令 \(dp[x][i]\) 表示 \(x\) 的子树中有多少距离 \(x\) 为 \(i\) 的点,\(f[x][i]\) 的子树中有多少点对的 \(lca\)(两个点到 \(lca\) 的距离均为 \(d\))距离 \(x\) 为 \(d-i\)。
这样,合并两个子树的时候考虑先用两个子树 \(dp\) 贡献出 \(f\),然后再各自对各自贡献相加即可。
但是这样做的时间复杂度 \(O(n^2)\)。
因为在这个做法中,第二维只和深度有关,所以考虑长链剖分,把轻儿子合并到重儿子上,因为这样做实际上于长度的值上,每一条长链只会被计算一次,所以时间复杂度为 \(O(n)\)。
具体的实现上可以使用指针移位来实现。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int dp = 0, c;
while (!isdigit(c = getchar())) c == '-' ? dp = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
dp ? x = -x : 0;
}
const int N = 1e5 + 7;
int n;
ll ans;
int len[N], son[N];
ll a[N << 2], *dp[N], *f[N], *now1, *now2;
struct Edge { int to, ne; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y) { g[++tot].to = y, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y) { addedge(x, y), addedge(y, x); }
inline void dfs1(int x, int fa = 0) {
len[x] = 1;
for fec(i, x, y) if (y != fa) dfs1(y, x), smax(len[x], len[y] + 1) && (son[x] = y);
}
inline void init(int x) {
dp[x] = now1, now1 += len[x];
now2 += len[x], f[x] = now2, now2 += len[x];
assert(now1 - a <= n), assert(now2 - a <= n * 3);
}
inline void dfs2(int x, int fa = 0) {
if (son[fa] != x) init(x);
if (son[x]) dp[son[x]] = dp[x] + 1, f[son[x]] = f[x] - 1, dfs2(son[x], x);
dp[x][0] = 1, ans += f[x][0];
for fec(i, x, y) if (y != fa && y != son[x]) {
dfs2(y, x);
for (int i = 0; i < len[y]; ++i) i && (ans += dp[x][i - 1] * f[y][i]), i + 1 < len[x] && (ans += f[x][i + 1] * dp[y][i]);
for (int i = 0; i < len[y]; ++i) {
if (i + 1 < len[x]) f[x][i + 1] += dp[x][i + 1] * dp[y][i], dp[x][i + 1] += dp[y][i];
if (i) f[x][i - 1] += f[y][i];
}
}
}
inline void work() {
dfs1(1);
now1 = a, now2 = a + n;
dfs2(1);
printf("%lld\n", ans);
}
inline void init() {
read(n);
int x, y;
for (int i = 1; i < n; ++i) read(x), read(y), adde(x, y);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP的更多相关文章
- BZOJ4543[POI2014]Hotel加强版——长链剖分+树形DP
题意参见BZOJ3522 n<=100000 数据范围增强了,显然之前的转移方程不行了,那么不妨换一种. 因为不能枚举根来换根DP,那么我们描述的DP方程每个点要计算三个点都在这个点的子树内的方 ...
- BZOJ.4543.[POI2014]Hotel加强版(长链剖分 树形DP)
题目链接 弱化版:https://www.cnblogs.com/SovietPower/p/8663817.html. 令\(f[x][i]\)表示\(x\)的子树中深度为\(i\)的点的个数,\( ...
- 【BZOJ4543】[POI2014]Hotel加强版 长链剖分+DP
[BZOJ4543][POI2014]Hotel加强版 Description 同OJ3522数据范围:n<=100000 Sample Input 7 1 2 5 7 2 5 2 3 5 6 ...
- BZOJ3522&4543 [POI2014]Hotel加强版 长链剖分
上上周见fc爷用长链剖分秒题 于是偷偷学一学 3522的数据范围很小 可以暴力枚举每个点作为根节点来dp 复杂度$O(n^2)$ 考虑令$f[x][j]$表示以$x$为根的子树内距离$x$为$j$的点 ...
- BZOJ4543 POI2014 Hotel加强版 【长链剖分】【DP】*
BZOJ4543 POI2014 Hotel加强版 Description 同OJ3522 数据范围:n<=100000 Sample Input 7 1 2 5 7 2 5 2 3 5 6 4 ...
- 【CF1009F】Dominant Indices(长链剖分优化DP)
点此看题面 大致题意: 设\(d(x,y)\)表示\(x\)子树内到\(x\)距离为\(y\)的点的个数,对于每个\(x\),求满足\(d(x,y)\)最大的最小的\(y\). 暴力\(DP\) 首先 ...
- CF1009F Dominant Indices——长链剖分优化DP
原题链接 \(EDU\)出一道长链剖分优化\(dp\)裸题? 简化版题意 问你每个点的子树中与它距离为多少的点的数量最多,如果有多解,最小化距离 思路 方法1. 用\(dsu\ on\ tree\)做 ...
- 2019.01.08 bzoj4543: [POI2014]Hotel加强版(长链剖分+dp)
传送门 代码: 长链剖分好题. 题意:给你一棵树,问树上选三个互不相同的节点,使得这个三个点两两之间距离相等的方案数. 思路: 先考虑dpdpdp. fi,jf_{i,j}fi,j表示iii子树中离 ...
- BZOJ4543 [POI2014]Hotel加强版
题意 有一个树形结构,每条边的长度相同,任意两个节点可以相互到达.选3个点.两两距离相等.有多少种方案? 数据范围:n<=100000 分析 参照小蒟蒻yyb的博客. 我们先考虑一个\(O(n^ ...
随机推荐
- Redis之Java客户端Jedis
导读 Redis不仅使用命令客户端来操作,而且可以使用程序客户端操作. 现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php.Node.js.Go等. 在官方网站里列一些Java ...
- beego 注解路由无效问题分析
问题描述:学习 beego 框架发现注解路由无效,除了不能找到路由外,未见任何异常. 问题解决:将配置文件中的 runmode 更改为 dev 模式. 问题分析: 如果没有设置过 runmode 不会 ...
- 【ABAP系列】SAP LSMW(摘自官网)
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP LSMW(摘自官网) 前 ...
- tensorflow和pytorch的区别
pytorch是动态框架,tensorflow是静态框架 针对tensorflow,我们先构造了一个计算图,构建完之后,这个计算图就不能改变了,我们再开启会话,输入数据,进行计算.那么这个流程就是固定 ...
- 20191003 尚硅谷Spring Cloud教学视频
视频信息 视频日期:2018-4-19 讲师:尚硅谷周阳 Spring Cloud版本:Dalston.RELEASE 当前版本:Greenwich SR3 微服务.微服务架构.Spring Clou ...
- Java相关面试题总结+答案(十)
[JVM] 194. 说一下 JVM 的主要组成部分?及其作用? 类加载器(ClassLoader) 运行时数据区(Runtime Data Area) 执行引擎(Execution Engine) ...
- Nacos1.1.3小试牛刀
什么是 Nacos(摘自https://nacos.io/zh-cn/docs/quick-start.html) Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性 ...
- JDK11 | 第二篇 : JShell 工具
文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/saHBSTo4OjsIIqv_ixigjg 一.简介 Java Shell工具是JDK1. ...
- SCUT - 271 - CC 非诚勿扰 - FFT
https://scut.online/p/271 第一次遇到没这么裸的,其实感觉到是卷积但是不知道怎么化.看来以后要多注意下标. #include <bits/stdc++.h> usi ...
- 2019 Multi-University Training Contest 2 - 1008 - Harmonious Army - 最大流
http://acm.hdu.edu.cn/showproblem.php?pid=6598 一开始就觉得是网络流,但是一直都不会怎么建图. 这里要考虑. 每一组边(u,v,a,b,c)建立如下的连接 ...