http://acm.timus.ru/problem.aspx?space=1&num=1039

1039. Anniversary Party

Time limit: 0.5 second
Memory limit: 8 MB

Background

The
president of the Ural State University is going to make an 80'th
Anniversary party. The university has a hierarchical structure of
employees; that is, the supervisor relation forms a tree rooted at the
president. Employees are numbered by integer numbers in a range from 1
to N, The personnel office has ranked each employee with a
conviviality rating. In order to make the party fun for all attendees,
the president does not want both an employee and his or her immediate
supervisor to attend.

Problem

Your task is to make up a guest list with the maximal conviviality rating of the guests.

Input

The first line of the input contains a number N. 1 ≤ N ≤ 6000.
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 the supervisor relation tree goes.
Each line of the tree specification has the form
<L> <K>
which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line
0 0

Output

The output should contain the maximal total rating of the guests.

Sample

input output
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
5

树上的树dp例题,f[i][0]表示以i为根不包含i可获得的最大价值,f[i][1]表示以i为根包含i在内可获得的最大价值。有f[i][0]+=MAX{f[son[i]][0],f[son[i]][1] } ,f[i][1]+=f[son[i]][0];

因为一旦包含i了显然不能包含i的儿子,所以不能加上f[son[i]][1],反之取二者中较大的就好了。

因为如何保存他们之间的关系想了半天,想用vector来着,看书上写的很简便,利用数组做邻接表处理。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3f
int c[],fa[],son[],bro[];
int dp[][];
void solve(int id)
{
dp[id][]=;
dp[id][]=c[id];
for(int i=son[id];i!=-;i=bro[i])
{
solve(i);
dp[id][]+=max(dp[i][],dp[i][]);
dp[id][]+=dp[i][];
}
}
int main()
{
int N,i,j,k;
while(cin>>N){
int a,b,root;
memset(fa,-,sizeof(fa));
memset(son,-,sizeof(son));
memset(bro,-,sizeof(bro));
for(i=;i<=N;++i) scanf("%d",c+i);
while(scanf("%d%d",&a,&b)&&(a||b)){
fa[a]=b;
bro[a]=son[b];
son[b]=a;
}
for(i=;i<=N;++i){
dp[i][]=,dp[i][]=c[i];
if(fa[i]==-) root=i;
}
solve(root);
printf("%d\n",max(dp[root][],dp[root][]));
}
return ;
}

ural 1039 树dp的更多相关文章

  1. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  2. 树形DP URAL 1039 Anniversary Party

    题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...

  3. CF456D A Lot of Games (字典树+DP)

    D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...

  4. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  5. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  6. HDU4276 The Ghost Blows Light SPFA&&树dp

    题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...

  7. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  8. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  9. bzoj 3572世界树 虚树+dp

    题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...

随机推荐

  1. Navicat for MySQL远程连接虚拟机

    在虚拟机中进入mysql mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT O ...

  2. python函数回顾:getattr()

    描述 getattr() 函数用于返回一个对象属性值. 语法 getattr 语法: getattr(object, name[, default]) 参数 object -- 对象. name -- ...

  3. LeetCode::Sort List 具体分析

    Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话.给链表排序,看到nlogn.我们 ...

  4. 剑指offer 面试24题

    面试24题: 题目:反转链表 题:输入一个链表,反转链表并输出反转后链表的头节点. 解题思路:注意反转时出现断裂现象,定义3个指针,分别指向当前遍历到的节点pNode.它的前一个节点pPrev及后一个 ...

  5. Django:popup弹出框简单应用实例

    效果:在p1.html页面点击,弹出p2的弹出框,填写数据,在 popup_response页面处理数据 1.url设置 urlpatterns = patterns( url(r'^p1.html' ...

  6. PAT 天梯赛 L1-020. 帅到没朋友 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L1-020 思路 对于每个 K >= 2 的朋友圈,里面的所有 ID 都用 MAP 标记一下 对于每个 K = ...

  7. NAS、SAN、DAS 说明

    NAS 说明 1.NAS(Network Attached Storage:网络附属存储) 2.NAS 是一种采用直接与网络介质相连的特殊设备实现数据存储的机制. 3.NAS本身能够支持多种协议(如N ...

  8. LVS/NAT 配置

    LVS/NAT 配置 实验环境 三台主机:Linux Centos 6.4 32位 调度器Director:192.168.1.160(内网IP).192.168.2.20(公网IP) HTTP真实服 ...

  9. pache—DBUtils框架简介、DbUtils类、QueryRunner类 、ResultSetHandler接口

    Apache—DBUtils框架简介.DbUtils类.QueryRunner类 .ResultSetHandler接口 commons-dbutils 是 Apache 组织提供的一个开源 JDBC ...

  10. python编程常见小技巧

    #主要是记录常见的小问题以及解决办法 ##1.复制的代码,经常出现TAB和空格不一致的情况 将tab或者空格删除,然后重新打出空格或者tab就可以了: ##2.python读取文件,经常出现的编码en ...