HDU 1520:Anniversary party(树形DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=1520
Anniversary party
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
题意:给出一个员工关系图(树),每个员工有一个上司和一个快乐度,现在要参加一个派对,不能让员工和直接上司一起到场,求现场能达到的最大快乐度是多少。
思路:比较水的题目,一个 t[i] 记录第 i 个员工出场的时候以 i 为根的树的总值,f[i] 记录第 i 个员工不出场的时候以 i 为根的树的总值。
f[i]的时候他的儿子可选可不选(一开始只考虑选的情况错了几次,有时候不选更优),t[i]的时候他的儿子一定不可选。
还有一个坑点是有多个case的。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 6010
struct node
{
int nxt, v;
}edge[N];
int head[N], tot;
int w[N], deg[N];
int f[N], t[N];
int s[N]; void add(int u, int v)
{
edge[tot].v = v;
edge[tot].nxt = head[u];
head[u] = tot++;
} void dfs(int s)
{
for(int i = head[s]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
dfs(v);
f[s] += max(t[v], f[v]); //可以选或者不选
t[s] += f[v];
}
} int main()
{
int n;
while(~scanf("%d", &n)) {
memset(f, , sizeof(f));
memset(t, , sizeof(t));
memset(deg, , sizeof(deg));
memset(head, -, sizeof(head));
tot = ;
for(int i = ; i <= n; i++) {
scanf("%d", &w[i]);
t[i] += w[i];
}
int u, v;
while() {
scanf("%d%d", &u, &v);
if(u + v == ) break;
add(v, u);
deg[u]++;
}
int cnt = ;
for(int i = ; i <= n; i++) {
if(deg[i] == ) {
s[cnt++] = i;
}
}
int ans = ;
for(int i = ; i < cnt; i++) {
dfs(s[i]);
ans += max(f[s[i]], t[s[i]]); //万一有很多个根
}
printf("%d\n", ans);
}
return ;
}
HDU 1520:Anniversary party(树形DP)的更多相关文章
- POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题
一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...
- HDU 1520 Anniversary party [树形DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给出n个带权点,他们的关系可以构成一棵树,问从中选出若干个不相邻的点可能得到的最大值为 ...
- hdu oj 1520 Anniversary party(树形dp入门)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 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 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1520 Anniversary party(第一道树形dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...
- HDU 1520 Anniversary party(DFS或树形DP)
Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...
- TTTTTTTTTTT hdu 1520 Anniversary party 生日party 树形dp第一题
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu Anniversary party 树形DP,点带有值。求MAX
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
随机推荐
- java类的加载、链接、初始化
JVM和类的关系 当我们调用JAVA命令运行某个java程序时,该命令将会启动一条java虚拟机进程,不管该java程序有多么复杂,该程序启动了多少个线程,它们都处于该java虚拟机进程里.正如前面介 ...
- 遇到could not find developer disk image 问题怎么解决
一般是设备的版本低于或者高于当前的xcode
- Effective C++ 2.构造 析构 赋值运算
//条款07:为多态基类声明virtual析构函数 // 1.若基类的析构函数不定义为虚函数,由于基类的指针或引用可以指向派生类的对象,则在删除基类对象的时候可能会出错,导致破坏数据结构. // 2. ...
- Lintcode: Majority Number II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size ...
- 显示和隐藏Mac下的 隐藏文件
显示:defaults write com.apple.finder AppleShowAllFiles -bool true隐藏:defaults write com.apple.finder Ap ...
- csuoj 1329: 一行盒子
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1329 1329: 一行盒子 Time Limit: 1 Sec Memory Limit: 12 ...
- java一般要点
1.String是引用类型. 2.char, short, byte在进行运算的时候会自动转换成int类型数据., 3.数据A 异或同一个数两次,得到的还是A 4.java的for循环,可以在前面加一 ...
- BZOJ 4052: [Cerc2013]Magical GCD
以一个数字开头的子序列的gcd种类不会超过logn种,因此去找相同gcd最长的位置,更新一下答案,复杂度O(nlogn^2) #include<cstdio> #include<al ...
- hdu5381 The sum of gcd
莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n)). 代码 #i ...
- (转)SQL对Xml字段的操作
T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...