[Codeforces Round #595 (Div. 3)] F. Maximum Weight Subset (树形DP)

题面:

[Codeforces Round #595 (Div. 3)] F. Maximum Weight Subset (树形DP)_#define

题意:

给定一棵树,每一个节点有一个权值\(a_i\),让你选择一个节点子集\(\mathit S\),使其集合中任意两点的距离大于\(\mathit k\),且集合这中点权总和最大。

思路:

考虑树形dp,状态定义:

\(dp[i][j]\) 为 以\(\mathit i\) 节点为根的子树的选取任意两点距离大于等于\(\mathit j\) 集合的最大权值和。

考虑转移:

  • 对于 以\(\mathit i\) 节点为根的子树,如果选择根节点\(\mathit i\),

那么遍历 i 的所有相邻非父顶点 j,将\(dp[i][0]\)加上与 j 距离至少为 k 的方案\(dp[j][k]\),即与 i 距离至少 k+1;

  • 对于 以\(\mathit i\) 节点为根的子树,如果不选择根节点\(\mathit i\),
    那么我们枚举两点最小距离\(\mathit j\),然后枚举任意两个其他的相邻非父顶点 x,y。
    两者之间的距离至少大于等于 k ,又因为当前的最小距离为 j,所以x节点贡献的方案是\(dp[x][j]\),
    \(\mathit y\) 节点贡献的方案是\(dp[y][max(j-1,k-j)]\)。

同时每一个节点方案处理完转移后要做一个后缀最大值,这样才能保证答案的正确性。

这样做的时间复杂度为:


\[O(n^3)\]


代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 210;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n, k;
std::vector<int> e[maxn];
int dp[maxn][maxn];
int a[maxn];
void dfs(int x, int pre)
{
dp[x][0] = a[x];
for (auto y : e[x]) {
if (y == pre) {
continue;
}
dfs(y, x);
dp[x][0] += dp[y][k];
}
for (int dis = 1; dis < n; dis++) {
for (auto i : e[x]) {
if (i == pre) {
continue;
}
int temp = dp[i][dis - 1];
for (int j : e[x]) {
if (j == pre || i == j) {
continue;
}
temp += dp[j][max(dis - 1, k - dis)];
}
dp[x][dis] = max(dp[x][dis], temp);
}
}
for (int dis = n - 1; dis >= 1; --dis) {
dp[x][dis - 1] = max(dp[x][dis - 1], dp[x][dis]);
}
}
int main()
{
#if DEBUG_Switch
freopen("D:\\code\\input.txt", "r", stdin);
#endif
//freopen("D:\\code\\output.txt","w",stdout);
n = readint();
k = readint();
repd(i, 1, n) {
a[i] = readint();
}
repd(i, 1, n - 1) {
int x = readint(), y = readint();
e[x].push_back(y);
e[y].push_back(x);
}
dfs(1, 0);
printf("%d\n", dp[1][0] );
return 0;
}