这题太恶心了,题意各种混乱。
#include
#include
#include
#define maxn 110
#define Clear(a,b) memset(a,b,sizeof(a))
using namespace std;
int emy[maxn],bug[maxn],dp[maxn][maxn],pos[maxn];
vector links[maxn];
int s;
int Max(int a,int b)
{
if (a > b) return a;
return b;
}
void dfs(int k,int fa)
{
int m = links[k].size(),son,i,j,c;
for(i = bug[k];i <= s;i++) dp[k][i] = pos[k];
if (k != 1 && m <= 1) {
/*
printf("%d:",k);
for(i = 1;i <= s;i++) printf(" %d",dp[k][i]);
printf("\n");
*/
//for(i = bug[k];i <= s;i++) dp[k][i] = pos[k];
return ;
}
for(i = 0;i < m;i++) {
son = links[k][i];
if (son == fa) continue;
dfs(son,k);
}
for(i = 0;i < m;i++) {
son = links[k][i];
if (son == fa) continue;
for(c = s;c >= bug[k];c--)
for(j = 1;j <= c;j++)
if (c - j >= bug[k])
dp[k][c] = Max(dp[k][c],dp[k][c-j] + dp[son][j]);
}
//for(i = 0;i < bug[k];i++) dp[k][i] = 0;
//for(i = s;i >= bug[k];i--) dp[k][i] = dp[k][i-bug[k]] + pos[k];
/*
printf("%d:",k);
for(i = 1;i <= s;i++) printf(" %d",dp[k][i]);
printf("\n");
*/
return ;
}
int work()
{
int n,i,a,b;
Clear(bug,0);
Clear(pos,0);
Clear(dp,0);
Clear(emy,0);
scanf("%d%d",&n,&s);
if (n == -1) return 0;
for(i = 1;i <= n;i++) links[i].clear();
for(i = 1;i <= n;i++) scanf("%d%d",&bug[i],&pos[i]);
for(i = 1;i <= n;i++) bug[i] = (bug[i] / 20) + (bug[i] % 20 != 0);
/*
for(i = 1;i <= n;i++) printf("%d ",bug[i]);
printf("\n");
*/
for(i = 1;i < n;i++) {
scanf("%d%d",&a,&b);
links[a].push_back(b);
links[b].push_back(a);
}
for(i = 1;i <= n;i++) if (i != 1 && links[i].size() == 1 && bug[i] == 0) bug[i] = 1;
dfs(1,0);
if (s == 0) {printf("0\n");return 1;}
printf("%d\n",dp[1][s]);
return 1;
}
int main()
{
while(work());
return 0;
}