Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9976    Accepted Submission(s): 4234

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
 
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;
dp[i][j] 表示第j个节点状态为i时i ->>[0,1]该节点选或不选,然后从根节点dfs dp就可以了。复杂度O(n);
 1 #include<stdio.h>
2 #include<math.h>
3 #include<queue>
4 #include<algorithm>
5 #include<string.h>
6 #include<iostream>
7 #include<stack>
8 #include<vector>
9 using namespace std;
10 typedef long long LL;
11 int val[7000];
12 vector<int>vec[7000];
13 int dp[2][7000];
14 int cnt[7000];
15 void dfs(int n);
16 int main(void)
17 {
18 int n;
19 while(scanf("%d",&n)!=EOF)
20 {
21 int i,j;
22 memset(cnt,0,sizeof(cnt));
23 for(i = 0; i < 7000; i++)
24 vec[i].clear();
25 memset(dp,0,sizeof(dp));
26 for(i = 1; i <= n; i++)
27 scanf("%d",&val[i]);
28 int ch,fa;
29 while(scanf("%d %d",&ch,&fa),ch!=0&&fa!=0)
30 {
31 cnt[ch] = 1;
32 vec[fa].push_back(ch);
33 }
34 int root;
35 for(i = 1; i <= n; i++)
36 {
37 if(!cnt[i])
38 {
39 root = i;
40 break;
41 }
42 }
43 dfs(root);
44 if(dp[1][root]<0&&dp[0][root]<0)
45 {
46 printf("0\n");
47 }
48 else
49 {
50 int ask = max(dp[1][root],dp[0][root]);
51 printf("%d\n",ask);
52 }
53 }
54 return 0;
55 }
56 void dfs(int n)
57 {
58 int sum1 = 0;
59 int sum2 = 0;
60 for(int i = 0; i < vec[n].size(); i++)
61 {
62 int id = vec[n][i];
63 dfs(id);
64 sum1 += dp[0][id];
65 sum2 += max(max(dp[1][id],dp[0][id]),0);
66 }
67 dp[0][n] = max(dp[0][n],sum2);
68 dp[0][n] = max(dp[0][n],sum1);
69 dp[1][n] = max(dp[0][n],val[n]);
70 dp[1][n] = max(dp[0][n],sum1+val[n]);
71 }

Anniversary party(hdu1520)的更多相关文章

  1. hdu1520 树形dp Anniversary party

    A - Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  2. HDU1520 Anniversary party 树形DP基础

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  3. hdu1520 Anniversary party

    Anniversary party HDU - 1520 题意:你要举行一个晚会,所有人的关系可以构成一棵树,要求上下级关系的人不能同时出现,每一个人都有一个rating值,要求使整个晚会的ratin ...

  4. HDU1520 Anniversary party —— 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  5. hdu1520 Anniversary party (树形dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1520题意:上司和直系下属不能同时参加party,求party的最大活跃值.输入: 输入n个 ...

  6. 树形dp Anniversary party(HDU1520)

    题意:给出一棵树,(上下级关系)每个节点都有一个权值,要求选出一些节点满足这些节点任意连个点都不是直接的上下级关系,可以得到的最大权值是多少? 分析:对于每个点有两个状态选或者不选,用状态数组dp[u ...

  7. HDU-1520 Anniversary party(树形DP)

    题目大意:一棵树,每个节点都带权.从中取出一些节点,并且子节点不能与父节点同时取,求能取得的最大值. 题目分析:定义状态dp(u,0/1)表示u点不取/取.则状态转移方程为: dp(u,1)=sum( ...

  8. hdu1520 Anniversary party 简单树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...

  9. 树状DP HDU1520 Anniversary party

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:职员之间有上下级关系,每个职员有自己的happy值,越高在派对上就越能炒热气氛.但是必须是 ...

随机推荐

  1. 27-Roman to Integer-Leetcode

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. 关于SQL中Union和Join的用法

    转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ...

  3. acid, acknowledge, acquaint

    acid sulphuric|hydrochloric|nitric|carbolic|citric|lactic|nucleic|amino acid: 硫|盐|硝|碳|柠檬|乳|核|氨基酸 王水是 ...

  4. Jenkins:参数化构建:分支|模块|回滚|打印日志

    @ 目录 多分支 安装Git Parameter Plug-In 配置参数 选择构建分支 分模块 前提 分模块build 参数配置 分模块shell脚本 mvn 的基本用法 分模块运行 Jenkins ...

  5. 零基础学习java------21---------动态代理,java8新特性(lambda, stream,DateApi)

    1. 动态代理 在一个方法前后加内容,最简单直观的方法就是直接在代码上加内容(如数据库中的事务),但这样写不够灵活,并且代码可维护性差,所以就需要引入动态代理 1.1 静态代理实现 在讲动态代理之前, ...

  6. 强化学习实战 | 表格型Q-Learning玩井字棋(二)

    在 强化学习实战 | 表格型Q-Learning玩井字棋(一)中,我们构建了以Game() 和 Agent() 类为基础的框架,本篇我们要让agent不断对弈,维护Q表格,提升棋力.那么我们先来盘算一 ...

  7. 【swift】长按事件绑定,平移滑动事件+坐标获取

    为何把这两个事件归类在一起? 我后来才明白,iOS有一个手势事件(UiGestureRecognizer) 事件里有7个功能,不过我只试过前两个,也就是标题的这两个(长按.平移滑动) UILongPr ...

  8. python下载openpyxl

    直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...

  9. 100个Shell脚本——【脚本9】统计ip

    [脚本9]统计ip 有一个日志文件,日志片段:如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com "/ ...

  10. mysql 索引 零记

    索引算法 二分查找法/折半查找法 伪算法 : 1. 前提,数据需要有序 2. 确定数据中间元素 K 3. 比如目标元素 A与K的大小 3.1 相等则找到 3.2  小于时在左区间 3.3  大于时在右 ...