如题,样例测试没问题,在另一个评测网站上也能80分,想着洛谷能下数据于是发现全wa。帮帮孩子吧!
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=501;
int in(){
int x=0;char ch=getchar();
if(ch<'0'||ch>'9') ch=getchar();
if(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct Edge{
int to,nex,w;
}edge[N*2];
int n,m,tot=0;
int headlist[N],dp[N][N];
void adds(int x,int y,int w){
edge[++tot].to=y;
edge[tot].nex=headlist[x];
edge[tot].w=w;
headlist[x]=tot;
}
void dfs(int u){
for(int i=headlist[u];i;i=edge[i].nex){
int v=edge[i].to,w=edge[i].w;
dfs(v);
for(int j=m;j>=1;j--){
for(int k=j;k>=1;k--){
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k-1]+w);
}
}
}
}
int main(){
n=in();m=in();
int x,w;
for(int i=1;i<=n;i++){
x=in();w=in();
adds(x,i,w);
}
dfs(0);
printf("%d",dp[0][m]);
return 0;
}