codevs 1380 没有上司的舞会 - 树形动态规划
Ural大学有N个职员,编号为1~N。他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。每个职员有一个快乐指数。现在有个周年庆宴会,要求与会职员的快乐指数最大。但是,没有职员愿和直接上司一起与会。
第一行一个整数N。(1<=N<=6000)
接下来N行,第i+1行表示i号职员的快乐指数Ri。(-128<=Ri<=127)
接下来N-1行,每行输入一对整数L,K。表示K是L的直接上司。
最后一行输入0,0。
输出最大的快乐指数。
各个测试点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 没有上司的舞会 - 树形动态规划的更多相关文章
- wikioi 1380 没有上司的舞会 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他 ...
- Codevs 1380 没有上司的舞会
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就 ...
- 树形DP--codevs 1380 没有上司的舞会
codevs 1380 没有上司的舞会 变式题目:给定一棵树每个点有一个点权,求一个独立集使得点权和最大,树上的独立集指的是选取树上的点,使尽量多的点不直接相连 时间限制: 1 s 空间限制: 1 ...
- CodeVS1380 没有上司的舞会 [树形DP]
题目传送门 没有上司的舞会 题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个 ...
- 『没有上司的舞会 树形DP』
树形DP入门 有些时候,我们需要在树形结构上进行动态规划来求解最优解. 例如,给定一颗\(N\)个节点的树(通常是无根树,即有\(N-1\)条无向边),我们可以选择任意节点作为根节点从而定义出每一颗子 ...
- 洛谷P1352 没有上司的舞会——树形DP
第一次自己写树形DP的题,发个博客纪念`- 题目来源:P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结 ...
- P1352 没有上司的舞会——树形DP入门
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员 ...
- P1352 没有上司的舞会&&树形DP入门
https://www.luogu.com.cn/problem/P1352 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的 ...
- [luogu]P1352 没有上司的舞会[树形DP]
本Lowbee第一次写树形DP啊,弱...一个变量写错半天没看出来...... 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点 ...
随机推荐
- linux、内核源码、内核编译与配置、内核模块开发、内核启动流程(转)
linux是如何组成的?答:linux是由用户空间和内核空间组成的为什么要划分用户空间和内核空间?答:有关CPU体系结构,各处理器可以有多种模式,而LInux这样的划分是考虑到系统的安全性,比如X86 ...
- webstorm的使用技巧——1
由于光标是在使用中突然发生变化,推测是碰到了快捷键,因此断定有快捷键可以修改.后来,无意中碰到了“Insert”键,于是光标立即发生了变化,“黑块矩形”变成“小竖线”.由此知道,insert键可以使光 ...
- 迷宫城堡--hdu1269(连通图)
题目链接 连通图模板题: #include<cstdio> #include<cstdlib> #include<cmath> #include<iost ...
- SQL 2
SQL SELECT 语句 SELECT 语句用于从数据库中选取数据. SQL SELECT 语句 SELECT 语句用于从数据库中选取数据. 结果被存储在一个结果表中,称为结果集. SQL SELE ...
- vue学习之二ECMAScript6标准
一.ECMAScript6标准简述 ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了.它的目标,是使得 JavaScr ...
- R中apply等函数用法[转载]
转自:https://www.cnblogs.com/nanhao/p/6674063.html 1.apply函数——对矩阵 功能是:Retruns a vector or array or lis ...
- oracle 之创建用户,表空间,授权,修改用户密码
1.创建表空间 create tablespace ilinkcargoagent logging datafile 'D:\app\Administrator\oradata\ilinkcargoa ...
- [LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- sql server删除重复数据,保留第一条
SELECT * FROM EnterpriseDataTools.Enterprise.CompanyMainwhere CompanyNo in (select CompanyNo from En ...
- Perl的debug小技巧
进入Perl的调试环境的命令: Perl -d perl_file 设置断点:b + perl代码中的行号. 执行到断点:c 表示continue until breakpoint. 执行下一条 ...