题目链接
bzoj1722: [Usaco2006 Mar] Milk Team Select 产奶比赛
题解
dp[i][j][0 / 1]
以i为根的子数中
相邻点对选了j个的最大价值
代码
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gc getchar()
#define pc putchar
inline int read() {
int x = 0,f = 1;
char c = gc;
while(c < '0' || c > '9') {if(c == '-') f = - 1; c = gc;}
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = gc;
return x * f;
}
#define LL long long
void print(int x) {
if(x >= 10) print(x / 10);
pc(x % 10 + '0');
}
const int maxn = 501;
const int maxm = 100007;
struct node {
int v,next;
} edge[maxn << 1];
int num = 0,head[maxn];
inline void add_edge(int u,int v) {
edge[++ num].v = v; edge[num].next = head[u];head[u] = num;
}
int val[maxn];
int n,x;
int f[maxn][maxn][2];
int siz[maxn];
void dfs(int x) {
siz[x] = 1;
f[x][0][0] = f[x][0][1] = 0;
for(int i = head[x];i;i = edge[i].next) {
int v = edge[i].v;
dfs(v);
siz[x] += siz[v];
for(int j = siz[x];j >= 0;-- j)
for(int k = 0;k <= std::min(j,siz[v]); ++ k) {
f[x][j][0] = std::max(f[x][j][0], f[x][j - k][0] + std::max(f[v][k][0],f[v][k][1]));
if(k) f[x][j][1] = std::max(f[x][j][1], f[x][j - k][1] + f[v][k - 1][1]);
f[x][j][1] = std::max(f[x][j][1], f[x][j - k][1] + f[v][k][0]);
}
}
for(int i = siz[x];i >= 0;-- i) f[x][i][1] += val[x];
}
int main() {
n = read(); x = read();
memset(f,-0x3f,sizeof f);
for(int i = 1;i <= n;++ i) {
val[i] = read();
add_edge(read(),i);
}
dfs(0);
for(int i = n;i >= 0;-- i) {
if(f[0][i][0] >= x) {
print(i); return 0;
}
}
pc('-'),pc('1');
return 0;
}