Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
1 #include<stdio.h> 2 #include<string.h> 3 int N; 4 int dp[6005][2]; 5 typedef struct node/*孩子兄弟法*/ 6 { 7 int brother; /*兄弟*/ 8 int next; /*孩子*/ 9 int havefather;/*是否有父亲,后面找根节点用*/ 10 }node; 11 node a[6005]; 12 int max(int a,int b) 13 { 14 return a>b?a:b; 15 } 16 void dfs(int x) 17 { 18 int i; 19 for(i=a[x].next;i!=0;i=a[i].brother) 20 { 21 dfs(i); 22 dp[x][1]+=dp[i][0]; /*x参加的时候,i肯定不能参加。*/ 23 dp[x][0]+=max(dp[i][0],dp[i][1]);/*x不参加的时候,i可以能参加,也不可不参加,选大的欢乐值*/ 24 } 25 } 26 int main() 27 { 28 int L,K; 29 int i; 30 int root; 31 while(scanf("%d",&N)!=EOF) 32 { 33 memset(dp,0,sizeof(dp)); 34 for(i=1;i<=N;i++) 35 { 36 scanf("%d",&dp[i][1]); 37 a[i].brother=0; 38 a[i].havefather=0; 39 a[i].next=0; 40 } 41 while(scanf("%d%d",&L,&K),L+K>0) 42 { 43 a[L].havefather=1; 44 if(a[K].next)/*要是有孩子了,后面插入孩子用兄弟形式表示*/ 45 { 46 a[L].brother=a[K].next; 47 a[K].next=L; 48 } 49 else 50 a[K].next=L; 51 } 52 for(i=1;i<=N;i++) 53 { 54 if(a[i].havefather==0) 55 { 56 root=i; 57 break; 58 } 59 60 } 61 dfs(root); 62 printf("%d\n",max(dp[root][1],dp[root][0])); 63 64 } 65 return 0; 66 }
Anniversary party解题报告,布布扣,bubuko.com
原文:http://www.cnblogs.com/love-willow/p/3606431.html