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. 解决fontawesome-webfont 被拦截的问题

    我们最近的项目是java web项目,前端采用了fontawesome-webfont,项目部署之后,图标都显示不出来,在网上学习了一大圈,找到了一个解决方案可行: web.xml中配置       ...

  2. jQuery手风琴广告展示插件

    效果说明:当鼠标移动到已折叠广告的标题后,折叠当前已展开的广告,并同步展开相应的折叠广告.这种Accordion效果,看似简单,但因为存在动画同步的问题,不能简单地用两个animate()来实现.必须 ...

  3. C# DllImport的用法

    大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功能 ...

  4. Python调试工具-Spyder

    OS:Windows 7 关键字:Python IDE, Spyder 1.安装工具pip:https://pip.pypa.io/en/latest/installing.html 下载 get-p ...

  5. Class.forName()的作用与使用总结(转载)

    转载自:Class.forName()的作用与使用总结 1.Class类简介: Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识.这项信息纪录了每个对象所属的类.虚拟机 ...

  6. 拖尾渲染器 Trail Renderer

    拖尾渲染器(Trail Renderer)用于制作跟在场景中的物体后面的拖尾效果来代表它们在到处移动. 必须给Materials一个材质渲染器设置的Colors才有效. 展示自己的一个demo...

  7. Quartz Scheduler 开发指南(1)

    Quartz Scheduler 开发指南(1) 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 实例化调度程序( ...

  8. 常用的四种CSS样式表格

    1. 单像素边框CSS表格 这是一个很常用的表格样式. [html] <style type="text/css"> table.gridtable { font-fa ...

  9. shell中的内建命令, 函数和外部命令

    转自shell中的内建命令, 函数和外部命令 Shell识别三种基本命令:内建命令.Shell函数以及外部命令: (1)内建命令就是由Shell本身所执行的命令.    有些命令是由于其必要性才内建的 ...

  10. Sql狗血的Bit类型赋值与取值

    Bit 数据类型在 SQL Server 数据库中以存储 1.0 进行存储. 往数据库中添加,修改 bit 类型的字段时,只能用 0 或者 1. 关于修改 Bit 类型的字段 1.若使用 SQL 语句 ...