题目描述 Description

Ural大学有N个职员,编号为1~N。他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。每个职员有一个快乐指数。现在有个周年庆宴会,要求与会职员的快乐指数最大。但是,没有职员愿和直接上司一起与会。

输入描述 Input Description

第一行一个整数N。(1<=N<=6000)
接下来N行,第i+1行表示i号职员的快乐指数Ri。(-128<=Ri<=127)
接下来N-1行,每行输入一对整数L,K。表示K是L的直接上司。
最后一行输入0,0。

输出描述 Output Description

输出最大的快乐指数。

样例输入 Sample Input

样例输出 Sample Output
 
数据范围及提示 Data Size & Hint

各个测试点1s


  按照这个关系建一棵树,然后进行树归,用f[i]表示i职员参加舞会的最大快乐指数之和,g[i]表示i职员不参加舞会的最大快乐指数之和。那么有f[i]为所有i的子节点的g[son[i]]的和,g[i]是i的子节点的g[son[i]]和f[son[i]]最大值的和。

  最后的答案在f[1]和g[1]中找最大值。

Code

 /**
* codevs
* Problem#1380
* Accepted
* Time:10ms
* Memory:492k
*/
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<ctime>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline boolean readInteger(T& u) {
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-') {
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
u *= aFlag;
ungetc(x, stdin);
return true;
} typedef class Edge {
public:
int end;
int next;
Edge(const int end = , const int next = ):end(end), next(next) { }
}Edge; typedef class MapManager {
public:
int ce;
int *h;
Edge *edge;
MapManager():ce(), h(NULL), edge(NULL) { }
MapManager(int points, int edges):ce() {
h = new int[(const int)(points + )];
edge = new Edge[(const int)(edges + )];
memset(h, , sizeof(int) * (points + ));
} inline void addEdge(int from, int end) {
edge[++ce] = Edge(end, h[from]);
h[from] = ce;
} Edge& operator [](int pos) {
return edge[pos];
}
}MapManager;
#define m_begin(g, i) (g).h[(i)] int n;
int *val;
MapManager g;
int *f, *g1;
int root; inline void init() {
readInteger(n);
val = new int[(const int)(n + )];
g = MapManager(n, n);
f = new int[(const int)(n + )];
g1 = new int[(const int)(n + )];
for(int i = ; i <= n; i++)
readInteger(val[i]);
int sum = ;
for(int i = , a, b; i < n; i++) {
readInteger(a);
readInteger(b);
sum += a;
g.addEdge(b, a);
}
root = n * (n + ) / - sum;
} void treedp(int node, int fa) {
g1[node] = ;
f[node] = val[node];
for(int i = m_begin(g, node); i != ; i = g[i].next) {
int& e = g[i].end;
if(e == fa) continue;
treedp(e, node);
g1[node] += max(g1[e], f[e]);
f[node] += g1[e];
}
} inline void solve() {
treedp(root, );
printf("%d", max(g1[root], f[root]));
} int main() {
init();
solve();
return ;
}

codevs 1380 没有上司的舞会 - 树形动态规划的更多相关文章

  1. wikioi 1380 没有上司的舞会 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他 ...

  2. Codevs 1380 没有上司的舞会

    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就 ...

  3. 树形DP--codevs 1380 没有上司的舞会

    codevs 1380 没有上司的舞会 变式题目:给定一棵树每个点有一个点权,求一个独立集使得点权和最大,树上的独立集指的是选取树上的点,使尽量多的点不直接相连  时间限制: 1 s  空间限制: 1 ...

  4. CodeVS1380 没有上司的舞会 [树形DP]

    题目传送门 没有上司的舞会 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个 ...

  5. 『没有上司的舞会 树形DP』

    树形DP入门 有些时候,我们需要在树形结构上进行动态规划来求解最优解. 例如,给定一颗\(N\)个节点的树(通常是无根树,即有\(N-1\)条无向边),我们可以选择任意节点作为根节点从而定义出每一颗子 ...

  6. 洛谷P1352 没有上司的舞会——树形DP

    第一次自己写树形DP的题,发个博客纪念`- 题目来源:P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结 ...

  7. P1352 没有上司的舞会——树形DP入门

    P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员 ...

  8. P1352 没有上司的舞会&&树形DP入门

    https://www.luogu.com.cn/problem/P1352 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的 ...

  9. [luogu]P1352 没有上司的舞会[树形DP]

    本Lowbee第一次写树形DP啊,弱...一个变量写错半天没看出来...... 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点 ...

随机推荐

  1. GCD之各种派发

    dispatch_apply的用法 并行模拟for循环,将指定的代码循环10次,一般会把这些代码附加到一个queue上,然后在 dispatch_apply里并行 dispatch_queue_t q ...

  2. php iconv() : Detected an illegal character in input string

    php iconv() : Detected an illegal character in input string_php技巧_脚本之家 https://www.jb51.net/article/ ...

  3. UNSIGNED command-line client

    High Performance MySQL, Third Edition by Baron Schwartz, Peter Zaitsev, and Vadim Tkachenko   There ...

  4. http_build_query

    http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( arra ...

  5. 【Python】详解Python多线程Selenium跨浏览器测试

    前言 在web测试中,不可避免的一个测试就是浏览器兼容性测试,在没有自动化测试前,我们总是苦逼的在一台或多台机器上安装N种浏览器,然后手工在不同的浏览器上验证主业务流程和关键功能模块功能,以检测不同浏 ...

  6. 【Pyton】【小甲鱼】魔法方法

    1.__init__ >>> class Rectangle: def __init__(self,x,y): self.x=x self.y=y def getPeri(self) ...

  7. 事务控制及try catch

    一.事务控制 BEGIN TRY BEGIN TRAN; DECLARE @aaa NVARCHAR(MAX); SET @aaa = 9 / 0; COMMIT TRAN;END TRYBEGIN ...

  8. PAT 1016 Phone Bills[转载]

    1016 Phone Bills (25)(25 分)提问 A long-distance telephone company charges its customers by the followi ...

  9. 查看项目中的laravel的版本

    方法1: 使用php artisan --version 方法2: 在项目文件中找vendor\laravel\framework\src\Illuminate\Foundation\Applicat ...

  10. soapUI-property Transfer

    1.1.1  Property Transfer 创建或双击现有的Property-Transfer TestStep将打开以下窗口: 左侧的列表显示了此TestStep中配置的传输,添加和管理所需的 ...