HDU 1520 - Anniversary party
http://blog.csdn.net/shuangde800/article/details/9731725
f[u][1] = val[u] + sum{f[v][0], v是u的儿子节点} //当选了u点时,它的儿子节点必须是不能选
http://www.cnblogs.com/E-star/archive/2012/02/11/2346758.html
Also check http://blog.csdn.net/crescent__moon/article/details/11651309
http://blog.csdn.net/shuangde800/article/details/9731725
Problem Description
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.
Input
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Output
Output should contain the maximal sum of guests' ratings.
Sample Input
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample Output
5
http://blog.csdn.net/shuangde800/article/details/9731725
树形dp的入门题
f[u][0]表示以u为顶点的子树,不选u点的情况下最大值
f[u][1]表示以u为顶点的子树,选u点的情况下最大值
那么,
f[u][0] = sum{ max{f[v][0], f[v][1]}, v是u的儿子节点}; //当不选u点时,它的儿子节点可以不选也可以选f[u][1]表示以u为顶点的子树,选u点的情况下最大值
那么,
f[u][1] = val[u] + sum{f[v][0], v是u的儿子节点} //当选了u点时,它的儿子节点必须是不能选
http://www.cnblogs.com/E-star/archive/2012/02/11/2346758.html
学校举行周年纪念日party,要求是直接上司和下属关系的员工不能同时参,给出树形关系图,求怎样邀请的人的列表是的总rating最大。每个人都对应着一个rating
首先建立无向图,然后以以1节点为根节点,dfs从叶子往根推。。
dp[i][0] 表示i节点不邀请是的最大rating,dp[i][1]表示i节点要请示最大的rating
转台转移方程
dp[i][0] += max(dp[son[i]][1],dp[son[i]][0]); i没被邀请,所以他的孩子可以邀请也可以不邀请,我们只要最大rating的。。。
dp[i][1] += dp[son[i]][0]; i已经被邀请了所以我们只能要他的孩子没被邀请时的最大rating
vector<int>g[maxn]; int dp[maxn][2],val[maxn]; bool visit[maxn]; void dfs(int rpos) { int i,num; visit[rpos] = true; int size = g[rpos].size(); if (size == 1 && rpos != 1) return ;//要注意根节点1的处理 for (i = 0; i < size; ++i) { num = g[rpos][i]; if(!visit[num]) { visit[num] = true; dfs(num); //状态转移 dp[rpos][0] += max(dp[num][1],dp[num][0]); dp[rpos][1] += dp[num][0]; } } } int main() { int n,i,x,y; while(~scanf("%d",&n)) { for (i = 1; i <= n; ++i) { scanf("%d",&val[i]); g[i].clear(); } while (scanf("%d%d",&x,&y)) { if(!x && !y) break; //建图 g[x].push_back(y); g[y].push_back(x); } //dp的初始化 for (i = 1; i <= n; ++i) { dp[i][0] = 0; dp[i][1] = val[i]; } memset(visit,false,sizeof(visit)); dfs(1); printf("%d\n",max(dp[1][0],dp[1][1])); } return 0; }
- int dp[maxn][2];
- int n,val[maxn];
- int H[maxn];
- vector<int>Edge[maxn];
- void dfs(int u,int fa)
- {
- int i,j;
- int mon=0;
- int non=0;
- for(i=0; i<Edge[u].size(); ++i)
- {
- int v=Edge[u][i];
- if(v==fa)continue;
- dfs(v,u);
- mon+=max(dp[v][1],dp[v][0]);
- non+=dp[v][0];
- }
- dp[u][1]=non+H[u];
- dp[u][0]=mon;
- }
- int main()
- {
- //freopen("D://input.txt","r",stdin);
- while(scanf("%d",&n)!=EOF)
- {
- int i,j,u,v;
- for(i=1; i<=n; i++)Edge[i].clear(),cin>>H[i];
- while(scanf("%d%d",&u,&v),(u||v))
- {
- Edge[u].push_back(v);
- Edge[v].push_back(u);
- }
- memset(dp,0,sizeof(int)*(n+1));
- dfs(1,-1);
- cout<<max(dp[1][1],dp[1][0])<<endl;
- }
- return 0;
- }
Also check http://blog.csdn.net/crescent__moon/article/details/11651309
http://blog.csdn.net/shuangde800/article/details/9731725