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;
}
posted @ 2019-09-21 07:26  hankeke  阅读(51)  评论(0编辑  收藏