Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3863   Accepted: 2172

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 N – 1 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 

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
题意:有一个大学的庆典晚会,想邀请一些在大学任职的人来参加,每个人有自己的搞笑值,但是现在遇到一个问题就是如果两个人之间有直接的上下级关系,那么他们中只能有一个来参加,求请来一部分人之后,搞笑值的最大是多少

思路:树形DP,在整个树上跑一遍dfs,设dp[s][0]表示不取s时以s为跟节点的子树的最大搞笑值,dp[s][1]表示取s时以s为跟节点的子树的最大搞笑值,则状态转移方程为:
          dp[s][0] += max(dp[u][0], dp[u][1]);
      dp[s][1] += dp[u][0];
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 6010
using namespace std;
int dp[MAXN][2], rat[MAXN];
int vis[MAXN], head[MAXN];
typedef struct{
int to, next;
}Edge;
Edge edge[MAXN];
void addedge(int u, int v, int k){
edge[k].to = v;
edge[k].next = head[u];
head[u] = k;
}
void dfs(int s){
dp[s][0] = 0;
dp[s][1] = rat[s];
for(int i = head[s];~i;i = edge[i].next){
int u = edge[i].to;
dfs(u);
dp[s][0] += max(dp[u][0], dp[u][1]);
dp[s][1] += dp[u][0];
}
}
int main(){
int n, u, v, father;
freopen("in.c", "r", stdin);
while(~scanf("%d", &n)){
memset(vis, 0, sizeof(vis));
memset(head, -1, sizeof(head));
for(int i = 1;i <= n;i ++) scanf("%d", rat+i);
for(int i = 1;i <= n-1;i ++){
scanf("%d%d", &u, &v);
addedge(v, u, i);
vis[u] = 1;
}
scanf("%d%d", &u, &v);
for(int i = 1;i <= n;i ++){
if(!vis[i]){
father = i;
break;
}
}
dfs(father);
printf("%d\n", max(dp[father][0], dp[father][1]));
}
return 0;
}

POJ 2342 (树形DP)的更多相关文章

  1. POJ 2342 树形DP入门题

    有一个大学的庆典晚会,想邀请一些在大学任职的人来參加,每一个人有自己的搞笑值,可是如今遇到一个问题就是假设两个人之间有直接的上下级关系,那么他们中仅仅能有一个来參加,求请来一部分人之后,搞笑值的最大是 ...

  2. POJ 2342 (树形DP)

    题目链接: http://poj.org/problem?id=2342 题目大意:直属上司和下属出席聚会.下属的上司出现了,下属就不能参加,反之下属参加.注意上司只是指直属的上司.每个人出席的人都有 ...

  3. poj 2342树形dp板子题1

    http://poj.org/problem?id=2342 #include<iostream> #include<cstdio> #include<cstring&g ...

  4. Anniversary party(POJ 2342 树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5767   Accepted: 3335 ...

  5. Fire (poj 2152 树形dp)

    Fire (poj 2152 树形dp) 给定一棵n个结点的树(1<n<=1000).现在要选择某些点,使得整棵树都被覆盖到.当选择第i个点的时候,可以覆盖和它距离在d[i]之内的结点,同 ...

  6. poj 1463(树形dp)

    题目链接:http://poj.org/problem?id=1463 思路:简单树形dp,如果不选父亲节点,则他的所有的儿子节点都必须选,如果选择了父亲节点,则儿子节点可选,可不选,取较小者. #i ...

  7. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...

  8. poj 3140(树形dp)

    题目链接:http://poj.org/problem?id=3140 思路:简单树形dp题,dp[u]表示以u为根的子树的人数和. #include<iostream> #include ...

  9. Strategic game(POJ 1463 树形DP)

    Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 De ...

  10. poj 3345 树形DP 附属关系+输入输出(好题)

    题目连接:http://acm.hust.edu.cn/vjudge/problem/17665 参考资料:http://blog.csdn.net/woshi250hua/article/detai ...

随机推荐

  1. html5在手机端关于 map area中的自适应

    https://github.com/stowball/jQuery-rwdImageMaps用这一个插件可自适应!!!

  2. 通过bat命令批量删除VS查找历史记录

    有时候我们会发现我们的VS查找下拉框里面有很多之前的搜索记录,想删除但是却没有地方删除.  网上的方法都是直接找到注册表HKEY_CURRENT_USER\Software\Microsoft\Vis ...

  3. ASP.Net MVC 生成安全验证码

    ---------html <td>验证码:</td>            <td>                <img src="/Logi ...

  4. 不使用BeanUtils,利用Java反射机制:表单数据自动封装到JavaBean

    在百度搜“java反射 将表单数据自动封装到javabean ”,第一页显示的都是一样的代码,都是利用导入第三方jar包<commons-beanutils>和<commons-lo ...

  5. Extension method for type

    扩展其实真的很简单 msdn是这样规定扩展方法的:"扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的. 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为 ...

  6. hdu 1695 GCD 莫比乌斯反演入门

    GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= ...

  7. poj 2104 K-th Number 划分树,主席树讲解

    K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...

  8. 2014年度辛星css教程夏季版第七节

    本小节我们研究浮动的问题,浮动使得布局更加灵活,虽然我们前面也有关于布局的讲解,但是它们提供的内容还是太有限了,要想获得更强大的布局,还必须使用浮动才能完成更灵活的布局. ***********浮动* ...

  9. joda jar日期处理类的学习

    转载:http://www.open-open.com/lib/view/open1348032952724.html 任何企业应用程序都需要处理时间问题.应用程序需要知道当前的时间点和下一个时间点, ...

  10. css中文本框与按钮对不齐解决方案

    我们先对对input标记设定样式,代码如下: html 代码 <form> <input type=”text” name=”text1” id=”text1” /> < ...