码迷,mamicode.com
首页 > 其他好文 > 详细

树形DP 没有上司的舞会

时间:2017-05-01 01:19:14      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:ring   clu   ack   algo   cstring   pac   algorithm   void   scanf   

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 
 7 using namespace std;
 8 int n;
 9 int root;
10 struct data{
11     int val;
12     int fa;
13     vector<int> ch;
14 }t[10010];
15 int f[10010][2];
16 
17 void dp(int x){
18     f[x][1]=t[x].val;
19     f[x][0]=0;
20     if(t[x].ch.empty()) return;
21     for(int i=0;i<t[x].ch.size();i++){
22         int q=t[x].ch[i];
23         dp(q);
24         f[x][0]=max(f[x][0],max(f[x][0]+f[q][1],f[x][0]+f[q][0]));//不选x 选儿子q?不选儿子q?
25         f[x][1]=max(f[x][1],f[x][1]+f[q][0]);//选x 必不可选其父和子 
26     }
27     return;
28 }
29 
30 int main(){
31     scanf("%d",&n);
32     for(int i=1;i<=n;i++) scanf("%d",&t[i].val);
33     int x=0,y=0;
34     for(int i=1;i<=n;i++){//=n吞0 0 
35         scanf("%d%d",&x,&y);
36         t[x].fa=y;
37         t[y].ch.push_back(x);
38     }
39     root=1;
40     while(t[root].fa) root=t[root].fa;
41     dp(root);
42     printf("%d\n",max(f[root][0],f[root][1]));
43     return 0;
44 }

 

树形DP 没有上司的舞会

标签:ring   clu   ack   algo   cstring   pac   algorithm   void   scanf   

原文地址:http://www.cnblogs.com/sdfzxh/p/6790802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!