bzoj4182 Shopping 点分治+单调队列优化多重背包
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=4182
题解
有一个很直观的想法是设 \(dp[x][i]\) 表示在以 \(x\) 为根的子树中选择一个总花费不超过 \(i\) 的以 \(x\) 为根的连通块的最大收益。
可惜,很不幸的是,这样做的时间复杂度无法像一般的树上背包和序列背包一样被保证。能够被保证复杂度的方法只有(可能是我只会)第二维与子树大小有关的方法,或者是将树转化成 dfs 序,然后在序列上做背包。
第二种方法具体的来说就是一次背包转移的时候,如果当前这一位选了物品,就直接从 \(i-1\) 转移,否则必须跳过这一棵子树,也就是从 \(i-siz[i]\) 转移。
但是这样只能求出来的是包含根的连通块的答案。于是我们可以采用点分治来弥补这个问题。
这样,总的时间复杂度为为 \(O(nm\log n)\),其中 \(nm\) 来源于单调队列优化的多重背包,\(\log 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 f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 500 + 7;
const int M = 4000 + 7;
const int INF = 0x3f3f3f3f;
int n, m, rt, mima, sum, dfc, ans;
int w[N], c[N], d[N];
int vis[N], siz[N], sq[N], dfn[N], q[M], p[M], dp[N][M];
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 getrt(int x, int fa = 0, int dep = 0) {
int f = 0; siz[x] = 1;
for fec(i, x, y) if (!vis[y] && y != fa) getrt(y, x), siz[x] += siz[y], smax(f, siz[y]);
smax(f, sum - siz[x]);
if (smin(mima, f)) rt = x;
}
inline void dfs(int x, int fa = 0) {
siz[x] = 1;
for fec(i, x, y) if (!vis[y] && y != fa) dfs(y, x), siz[x] += siz[y];
dfn[x] = ++dfc, sq[dfc] = x;
}
inline void calc(int x) {
dfc = 0, dfs(x);
for (int i = 1; i <= dfc; ++i) {
int x = sq[i], hd = 1, tl = 0;
for (int j = 0; j < c[x]; ++j) {
hd = 1, tl = 0;
for (int k = 0; k * c[x] + j <= m; ++k) {
int v = k * c[x] + j, y = dp[i - 1][v] - k * w[x];
while (hd <= tl && q[hd] < k - d[x]) ++hd;
if (hd <= tl) dp[i][v] = std::max(p[hd] + k * w[x], dp[i - siz[x]][v]);
else dp[i][v] = dp[i - siz[x]][v];
while (hd <= tl && y >= p[tl]) --tl;
q[++tl] = k, p[tl] = y;
}
}
}
smax(ans, dp[dfc][m]);
}
inline void solve(int x) {
vis[x] = 1, calc(x);
for fec(i, x, y) if (!vis[y]) {
mima = sum = siz[y];
getrt(y), solve(rt);
}
}
inline void work() {
mima = sum = n;
getrt(1), solve(rt);
printf("%d\n", ans);
}
inline void cls() {
memset(vis, 0, sizeof(vis));
memset(head, 0, sizeof(head));
ans = tot = 0;
}
inline void init() {
cls();
read(n), read(m);
for (int i = 1; i <= n; ++i) read(w[i]);
for (int i = 1; i <= n; ++i) read(c[i]);
for (int i = 1; i <= n; ++i) read(d[i]);
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
int T;
read(T);
while (T--) {
init();
work();
}
fclose(stdin), fclose(stdout);
return 0;
}
bzoj4182 Shopping 点分治+单调队列优化多重背包的更多相关文章
- POJ 1742 (单调队列优化多重背包+混合背包)
(点击此处查看原题) 题意分析 给你n种不同价值的硬币,价值为val[1],val[2]...val[n],每种价值的硬币有num[1],num[2]...num[n]个,问使用这n种硬币可以凑齐[1 ...
- [Bzoj4182]Shopping(点分治)(树上背包)(单调队列优化多重背包)
4182: Shopping Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 374 Solved: 130[Submit][Status][Disc ...
- HDU 2191 - 单调队列优化多重背包
题目: 传送门呀传送门~ Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种 ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- [小明打联盟][斜率/单调队列 优化dp][背包]
链接:https://ac.nowcoder.com/acm/problem/14553来源:牛客网 题目描述 小明很喜欢打游戏,现在已知一个新英雄即将推出,他同样拥有四个技能,其中三个小技能的释放时 ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- bzoj1531[POI2005]Bank notes 单调队列优化dp
1531: [POI2005]Bank notes Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 559 Solved: 310[Submit][Sta ...
- 算法笔记--单调队列优化dp
单调队列:队列中元素单调递增或递减,可以用双端队列实现(deque),队列的前面和后面都可以入队出队. 单调队列优化dp: 问题引入: dp[i] = min( a[j] ) ,i-m < j ...
- [DP浅析]线性DP初步 - 2 - 单调队列优化
目录 #0.0 前置知识 #1.0 简单介绍 #1.1 本质 & 适用范围 #1.2 适用方程 & 条件 #2.0 例题讲解 #2.1 P3572 [POI2014]PTA-Littl ...
随机推荐
- Android解析编译之后的所有文件(so,dex,xml,arsc)格式
我们在之前一篇一篇介绍了如何解析Android中编译之后的所有文件格式,所有的工作都完成了,这里我们就来做个总结,我们为什么要做这些工作: 第一篇:解析so文件格式 点击进入 这里我们解析so文件,主 ...
- windows 系统再重启后,USB口失效(鼠标、U盘都无法识别)的过程及解决方法
今天都差点忘记写随笔.今天在工作中,将电脑重启了一次,悲催了.重启完成后,鼠标无法使用了.最初认为 鼠标的问题,就一直"砸",但后来换了鼠标,仍然不能使用,开始认为没这边简单,拿出 ...
- String Problem
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- SLF4J: The requested version 1.6 by your slf4j binding is not compatible with [1.5.5, 1.5.6]
SLF4J:你的SLF4J绑定请求的1.6版不兼容[1.5.5,1.5.6] 在lib中,此时slf4j的版本是1.5.6,而slf4j-log4j的版本是1.6.由于版本的不兼容性,导致了这个错误. ...
- 第九届ECNU Coder F.蚂蚁(栈)
题目链接:http://acm.ecnu.edu.cn/contest/16/problem/F/ 题目: F. 蚂蚁 Time limit per test: 0.5 seconds Time li ...
- Step3 - How to: Host and Run a Basic Windows Communication Foundation Service
This is the third of six tasks required to create a Windows Communication Foundation (WCF) applicati ...
- HTTP和HTTPS协议,详解
大纲 一.前言: 先来观察这两张图,第一张访问域名http://www.12306.cn,谷歌浏览器提示不安全链接,第二张是https://kyfw.12306.cn/otn/regist/init, ...
- Python基础学习:operator模块
operator——函数的标准操作 转自:https://blog.csdn.net/zhtysw/article/details/80510113 代码资源:Lib/operator.py oper ...
- Integer自动装箱和拆箱
Integer a=3; => Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...
- 《图解设计模式》读书笔记4-1 Bridge模式
目录 概念 代码 角色 类图 想法 概念 Bridge模式即桥接模式.顾名思义,这个模式的作用是将类的功能层次结构和类的实现层次结构连接起来. 功能层次结构 Something -SomethingG ...