标签:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6011 Accepted Submission(s): 3555
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <set> 9 using namespace std; 10 11 #define N 205 12 13 int max(int x,int y){return x>y?x:y;} 14 int min(int x,int y){return x<y?x:y;} 15 int abs(int x,int y){return x<0?-x:x;} 16 17 vector<int>ve[N]; 18 int dp[N][N]; 19 int n, m; 20 int val[N]; 21 22 void dfs(int u){ 23 int i, j, k, v; 24 dp[u][1]=val[u]; 25 for(i=0;i<ve[u].size();i++){ 26 v=ve[u][i]; 27 dfs(v); 28 for(j=m+1;j>=2;j--){//倒着for是为了不重复 29 for(k=1;k<j;k++){ 30 dp[u][j]=max(dp[u][j],dp[u][k]+dp[v][j-k]); 31 } 32 } 33 } 34 } 35 36 main() 37 { 38 int i, j, k, u, v; 39 while(scanf("%d %d",&n,&m)==2){ 40 if(!n&&!m) break; 41 for(i=0;i<=n;i++) ve[i].clear(); 42 for(i=1;i<=n;i++){ 43 scanf("%d %d",&u,&val[i]); 44 ve[u].push_back(i); 45 } 46 val[0]=0; 47 memset(dp,0,sizeof(dp)); 48 dfs(0); 49 printf("%d\n",dp[0][m+1]); 50 } 51 }
标签:
原文地址:http://www.cnblogs.com/qq1012662902/p/4648106.html