Anniversary party

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

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
 
Source
 
基础树形dp
每个点的子节点用vector保存下来;
d[i][0] 表示不选择此节点的最大值
d[i][1] 表示不选择此节点的最大值
d[root][0] += max(d[v[root][i]][1], d[v[root][i]][0]); 
d[root][1] += d[v[root][i]][0];
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define sfl(n) scanf("%lld", &n)
#define pfi(n) printf("%d\n", n)
#define pfl(n) printf("%lld\n", n)
#define MAXN 6005
int w[MAXN];
bool f[MAXN] = {false};
vector<int> v[MAXN];
int d[MAXN][];
bool vis[MAXN] = {false};
void dp(int root)
{
if(vis[root]) return ;
vis[root] = true;
int siz = v[root].size();
repu(i, , siz) dp(v[root][i]); repu(i, , siz)
{
d[root][] += max(d[v[root][i]][], d[v[root][i]][]);
d[root][] += d[v[root][i]][];
}
return ;
}
int main()
{
int n;
while(~sfi(n))
{
_cle(vis, false);
_cle(f, false);
repu(i, , n + )
{
sfi(w[i]);
v[i].clear();
d[i][] = ;
d[i][] = w[i];
}
int x, y;
while(sfi(x), sfi(y), x + y)
{
v[y].push_back(x);
f[x] = true;
}
int root;
repu(i, , n + ) if(!f[i])
{
root = i;
break;
}
dp(root);
pfi(max(d[root][], d[root][]));
//repu(i, 1, n + 1) printf("%d %d\n", d[i][0], d[i][1]);
} return ;
}
 

hdu 1520的更多相关文章

  1. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  2. 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 ...

  3. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  4. HDU 1520:Anniversary party(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description   There i ...

  5. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  6. HDU 1520 Anniversary party [树形DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...

  7. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. hdu 1520(简单树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:dp[u][0]表示不取u的最大价值,dp[u][1]表示取u的最大价值,于是有dp[u] ...

  9. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  10. hdu 1520 树形DP基础

    http://acm.hdu.edu.cn/showproblem.php?pid=1520 父节点和子节点不能同时选. http://blog.csdn.net/woshi250hua/articl ...

随机推荐

  1. ServiceStack.OrmLite 学习笔记7-复杂点的使用1

    复杂点的使用1 先看看这2个类 class Customer { public int Id { get; set; } ... } class CustomerAddress { public in ...

  2. linux 命令行模式下,浏览网页

    Ubuntu自带最新版的Gnome桌面,拥有大量的服务和桌面应用程序,让您仅通过一张安装光盘就可以体验到无比舒适的操作环境.下文介绍的在ubuntu下使用终端命令行上网的方法. 第一步,需要安装一个名 ...

  3. 自定义表单input

    我想实现下面这个效果?应该怎么写最方便呢?最有效,兼容性最好呢 我使用<p>标签套lable,加input的组合,p标签绝对定位,input标签铺满,用padding填充. 主要css . ...

  4. JavaScript算法题之–随机数的生成

    JavaScript算法题之–随机数的生成 需求描述:从一组有序的数据中生成一组随机并且不重复的数,类似于简单的抽奖程序的实现. 先来生成一个有序的数组: 1 var arr = [], 2      ...

  5. [转载] 单表60亿记录等大数据场景的MySQL优化和运维之道 | 高可用架构

    原文: http://mp.weixin.qq.com/s?__biz=MzAwMDU1MTE1OQ==&mid=209406532&idx=1&sn=2e9b0cc02bdd ...

  6. elastic

    学习链接 http://rfyiamcool.blog.51cto.com/1030776/1420811?utm_source=tuicool&utm_medium=referral

  7. Ubuntu Server14.04 32位安装odoo8.0简单方法

    一.wget -O - https://nightly.odoo.com/odoo.key | apt-key add - 二.echo "deb http://nightly.odoo.c ...

  8. Java编程思想学习笔记_6(并发)

    一.从任务中产生返回值,Callable接口的使用 Callable是一种具有泛型类型参数的泛型,它的类型参数表示的是从方法call返回的值,而且必须使Executor.submit来去调用它.sub ...

  9. spring data jpa 创建方法名进行简单查询

    版权声明:本文为博主原创文章,未经博主允许不得转载. spring data jpa 可以通过在接口中按照规定语法创建一个方法进行查询,spring data jpa 基础接口中,如CrudRepos ...

  10. javascript强制转换详解

    转换成数值 Number函数强制转换成数值 数值->转换成原来的值 字符串->如果可以解析为数值,则转换成数值:否则转换成NaN或者0 true->1,falSe->0 und ...