1 条题解

  • 0
    @ 2023-4-27 21:10:56
    #include<bits/stdc++.h>
    #define re register
    #define N 105
    using namespace std;
    vector<int> son[N];
    int f[N][N],s[N],n,m;
    inline void dp(int x)
    {//f[x][0]无意义,此处不考虑 ,故最小单位从f[x][1]开始 
    	f[x][1]=s[x];
    	for(re int i=0;i<son[x].size();++i){
    		int y=son[x][i];
    		dp(y);
    		for(re int t=m;t>=1;--t)//分组背包
    			for(re int j=1;j<t;++j)
    				f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
    	}
    } 
    int main()
    {
    	scanf("%d%d",&n,&m);
    	m++;//后面操作中包括了0结点,故必须+1 
    	for(re int i=1;i<=n;++i){
    		int x;
    		scanf("%d%d",&x,&s[i]);
    		son[x].push_back(i);
    	}
    	memset(f,-1,sizeof(f));//-INF
    	dp(0);//虚拟结点 :化森林为树 
    	printf("%d\n",f[0][m]);  
    	return 0;
    }
    
    • 1

    信息

    ID
    633
    时间
    1000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    3
    已通过
    3
    上传者