LJJ爱数树

  1. 状态为f[i][j][k],和g[i][j][k], i为i子树内,j为放置的摄像头个数,f中k为内部向外延伸的长度,g中k为外部向内延伸长度
  2. 当内部摄像头j*k>=siz[i]时一定可以全覆盖,要特殊记录,这也是复杂度为n方的关键
  3. 三维太多,用encode和decode压缩空间
  4. 考虑转移
    因为太难了所以代码还没写完丢这里
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 1e3 + 10;
int n, k, r, w[N], siz[N], g[N][N], f[N][N*3], tmp[N], a[N];
int tf[N], tg[N], pmxfu[N], pmxgu[N], pmxfv[N], pmxgv[N], sum[N];
vector<int>E[N];

int encode(int x, int y) {
	return x * N + y;
}

// f 为溢出的,g为提供的
void dfs(int u, int fa) {
	siz[u] = 1;
	sum[u] = a[u];
	//初始化还不会写
	
	for (int v : E[u]) {
		if (v == fa) continue;
		dfs(v, u);
		for (int i = 1; i <= min(siz[u], k); i++) {
			for (int j = 1; j <= min(siz[v], k) && j + i <= k; j++) {
				for (int x = 0; x <= r; x++) { 
					int tmp = encode(i + j, x);
					// 全覆盖
					if (x * (i + j) >= siz[u] + siz[v]) {
						tf[tmp] = tg[tmp] = sum[u] + sum[v];
						continue;
					}
					// fu 与 fv合并为tf
					tf[tmp] = max(tf[tmp], max(pmxfu[x] + f[v][encode(j, x + 1)], f[u][encode(i, x)] + pmxfv[x + 1]));
					// gu 与 gv 合并为tg
					tg[tmp] = max(tg[tmp], max(pmxgu[x] + g[v][encode(j, x - 1)], g[u][encode(i, x)] + pmxgv[x - 1]));
					// fu 与 gv 合并为 tf
					tf[tmp] = max(tf[tmp], f[u][encode(i, x)] + pmxgv[x - 1]);
					// fu 与 gv 合并为 tg
					tg[tmp] = max(tg[tmp], g[v][encode(i, x - 1)] + pmxfu[x]);
					// fv 与 gu 合并为 tf
					tf[tmp] = max(tf[tmp], f[v][encode(i, x + 1)] + pmxgu[x]);
					// fv 与 gu 合并为 tg
					tg[tmp] = max(tg[tmp], g[v][encode(i, x)] + pmxfv[x - 1]);
				}
			}
		}
		siz[u] += siz[v];
		sum[u] += sum[v];
		for (int i = 1; i <= k; i++) {
			for (int x = 0; x <= r; x++) { 
				int tmp = encode(i, x);
				f[u][tmp] = tf[tmp]; tf[tmp] = 0;
				g[u][tmp] = tg[tmp]; tg[tmp] = 0;

			}
		}
	}
}


void solve() {
	cin >> n >> k >> r;
	for (int i = 1; i <= n; i++) cin >> w[i];
	for (int i = 1; i < n; i++) {
		int u, v; cin >> u >> v;
		E[u].push_back(v); E[v].push_back(u);
	}
	dfs(1, 0);
	for (int i = 1; i <= n; i++) E[i].clear();
}
int main() {
	int T; cin >> T;
	while (T--) solve();
}
posted @ 2024-12-03 14:35  lyrrr  阅读(5)  评论(0编辑  收藏  举报