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. 我的Android进阶之旅------>Android使用cmd窗口进行adb logcat时出现中文乱码问题的解决办法

    今天用CMD命令进行adb logcat时显示一堆的乱码,乱码如下: C:\Users\Administrator>adb logcat -s logcat --------- beginnin ...

  2. Chrome 正在受到自动化软件控制 – 解决办法

    1.在这个文件下修改一行代码就可以了,文件路径E:\Miniconda3\envs\env3\Lib\site-packages\selenium\webdriver\chrome\options.p ...

  3. echarts3.8.4实现模拟迁移

    动态接受城市的经纬度https://zhidao.baidu.com/question/1384875311724922940.html 调用百度api获得ip对应的城市https://www.cnb ...

  4. Redis慢查询,redis-cli,redis-benchmark,info

    一.慢查询: 1.慢查询的作用:通过慢查询分析,找到有问题的命令进行优化. 2.慢查询的redis的配置参数: slowlog-log-slower-than 慢查询预设阈值(单位是微秒1秒=1000 ...

  5. 算法寒假实习面试经过之 滴滴(电话一面二面 offer)

    一面:1h 介绍比赛项目. lr与xgb的区别? xgb 为什么不用归一化,onehot? xgb 与 gbdt的区别. 做这些比赛你们的优势在哪,既然全是相同的套路. RCNN的原理, CNN的原理 ...

  6. c# 虚方法(virtual)与 多态(Polymorphism)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //虚方法(virtual) ...

  7. SOA和微服务之间的区别

    近几年,我们有很多文章对SOA和微服务之间的不同点和相似点进行了分析.有些人认为SOA有很多地方是值得微服务学习的,而有些人则认为区别对待微服务和SOA会更好.而Neal Ford认为,将单体迁移到面 ...

  8. 方法——<37>

    1,返回url参数 /* * 返回参数值 * @method getUrlPara * @papram {string},url中参数名 * @return {string},url中参数值 * */ ...

  9. 建议47:使用logging记录日志信息

    # -*- coding:utf-8 -*- ''' Python中自带的logging 模块提供了日志功能,它将logger 的level 分为5 个级别 DEBUG 详细的信息,在追踪问题的时候使 ...

  10. [GUI] Linux中的图形管理

    转:http://www.cnblogs.com/yongpenghan/p/4555619.html 做了一段时间linux下与QT事件相关的工作,经常会遇到X11,总是苦于无法完全理解其与linu ...