标签:style blog http io ar color os sp java
3 2 0 1 0 2 0 3 7 4 2 2 0 1 0 4 2 1 7 1 7 6 2 2 0 0
5 13
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 210; 18 struct arc{ 19 int to,next; 20 arc(int x = 0,int y = -1){ 21 to = x; 22 next = y; 23 } 24 }; 25 arc e[maxn*maxn]; 26 int dp[maxn][maxn],head[maxn],val[maxn],n,m,tot; 27 bool vis[maxn]; 28 void add(int u,int v){ 29 e[tot] = arc(v,head[u]); 30 head[u] = tot++; 31 } 32 void dfs(int u){ 33 vis[u] = true; 34 dp[u][1] = val[u]; 35 for(int i = head[u]; ~i; i = e[i].next){ 36 if(vis[e[i].to]) continue; 37 dfs(e[i].to); 38 for(int j = m+1; j >= 1; j--){ 39 for(int k = 1; k < j; ++k) 40 dp[u][j] = max(dp[u][j],dp[u][j-k] + dp[e[i].to][k]); 41 } 42 } 43 } 44 int main() { 45 int u,v; 46 while(scanf("%d %d",&n,&m),n||m){ 47 memset(head,-1,sizeof(head)); 48 memset(vis,false,sizeof(vis)); 49 for(int i = 1; i <= n; ++i){ 50 scanf("%d %d",&u,val+i); 51 add(u,i); 52 } 53 memset(dp,0,sizeof(dp)); 54 dfs(0); 55 printf("%d\n",dp[0][m+1]); 56 } 57 return 0; 58 }
标签:style blog http io ar color os sp java
原文地址:http://www.cnblogs.com/crackpotisback/p/4108102.html