题目描述
某大学有N个职员,编号为1~N。他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但是呢,如果某个职员的上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了。所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数。

输入输出格式
输入格式:
第一行一个整数N。(1<=N<=6000)

接下来N行,第i+1行表示i号职员的快乐指数Ri。(-128<=Ri<=127)

接下来N-1行,每行输入一对整数L,K。表示K是L的直接上司。

最后一行输入0 0

输出格式:
输出最大的快乐指数。
【分析】:
那么考虑树形DP的状态设计,一般与子树有关,即求出子树的答案然后合并上去
考虑在合并的时候,会影响答案合并的状态,就是子树的根节点选或者不选了,选了的话当前节点就不能选,两者的答案不一样当然要分开统计
于是得出状态设计 : f(i,0/1)
表示第i个子树内,i这个点选或者不选的最大值
f(i,0) = sigma max(f(v,0),f(v,1)) + ai,这是由于只要根不选,子树选不选都可以的缘故
f(i,1) = sigma f(v,0) ,这是由于根选了,所有子树的根都不能选的缘故

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int mod = 10056;
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int h[maxn];
int v[maxn];
int n;
vector<int> son[maxn];
int f[maxn][2];
void dfs(int x)
{f[x][0]=0;f[x][1]=h[x];for(int i=0;i<son[x].size();i++){int y=son[x][i];dfs(y);f[x][0]+=max(f[y][0],f[y][1]);f[x][1]+=f[y][0];}
}
int main()
{cin>>n;rep(i,1,n) cin>>h[i];rep(i,1,n-1){int x,y;cin>>x>>y;son[y].push_back(x);v[x]=1;}int root;rep(i,1,n){if(!v[i]){root=i;break;}}dfs(root);cout<<max(f[root][0],f[root][1])<<endl;
}

HDU 1520 【链式前向星版本】
链接

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 100010 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int mod = 10056;
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int h[maxn];
int v[maxn];
int n;
int f[maxn][2];
int head[maxn];
struct node
{int to,nxt;
}e[maxn*2];
int tot=0;
void add(int u,int v)
{tot++;e[tot].to=v;e[tot].nxt=head[u];head[u]= tot;
}
void init()
{tot=0;ms(head,-1);ms(f,0);ms(v,0);
}
void dfs(int u,int fa)
{//f[u][0]=0;f[u][1]=h[u];for(int i=head[u]; i!=-1; i=e[i].nxt){int v=e[i].to;if(v==fa) continue;dfs(v,u);f[u][0] += max(f[v][0],f[v][1]);f[u][1] += f[v][0];}
}
int main()
{while(cin>>n){init();rep(i,1,n) cin>>h[i];rep(i,1,n-1){int x,y;cin>>x>>y;add(x,y), add(y,x);v[x]++;}scanf("\n0 0");int root;rep(i,1,n)if(!v[i]){root=i;break;}dfs(root,root);cout<<max(f[root][0],f[root][1])<<endl;}}
/*
Sample Input
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 05
*/

转载于:https://www.cnblogs.com/Roni-i/p/9409764.html

洛谷 P1352 没有上司的舞会【树形DP/邻接链表+链式前向星】相关推荐

  1. 洛谷 P1352 没有上司的舞会(树形 DP)

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

  2. 洛谷 P1352 没有上司的舞会

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

  3. 洛谷P1352 没有上司的舞会(树形DP水题)

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

  4. 洛谷P1352 没有上司的舞会题解

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

  5. 洛谷 P1352 没有上司的舞会(树形dp)

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

  6. 洛谷 [P1352] 没有上司的舞会

    树型DP 一个人不能和他的直接上司一起去,那么就分别保存这个人去和不去的最大值 注意转移方程 #include <iostream> #include <cstring> #i ...

  7. 蓝桥杯 强力党逗志芃 链式前向星结构的树形dp

    // 蓝桥杯 强力党逗志芃 // 采用树形dp,树以链式前向星形式存储#include <iostream> using namespace std;int power[205]; // ...

  8. AcWing285. 没有上司的舞会(树形DP)题解

    题目传送门 题目描述 Ural大学有N名职员,编号为1~N. 他们的关系就像一棵以校长为根的树,父节点就是子节点的直接上司. 每个职员有一个快乐指数,用整数 HiHi 给出,其中 1≤i≤N. 现在要 ...

  9. [洛谷 P4084 USACO17DEC] Barn Painting G (树形dp经典)

    [洛谷 P4084 USACO17DEC] Barn Painting G 题目链接 大致题意: 给定一颗N个节点组成的树,3种颜色,其中K个节点已染色,要求任意两相邻节点颜色不同,求合法染色方案数 ...

最新文章

  1. Hadoop: MapReduce2的几个基本示例
  2. 一图简述大数据技术生态圈
  3. 数据湖之iceberg系列(三)iceberg快速入门
  4. Product search parameter的determine逻辑
  5. JavaScript学习第一天(一)
  6. php测试插入,php – 使用Symfony测试数据库插入
  7. Docker 基本用法
  8. 让元素固定_原神雷元素不如火元素吗?阵容搭配与圣遗物强化攻略
  9. 股票价格跨度--单调栈
  10. myeclipse6.5集成freemaker
  11. 疑似Bug:简书安卓10.7首页搜索关键字屏蔽敏感词不彻底
  12. Python语言学习 (二)1.1
  13. iOS开发之App从点击到启动
  14. Castle ActiveRecord学习实践(1)入门
  15. java-循环变量定义问题
  16. 常用制图工具Visio-ProcessOn
  17. VS2015 密钥(证实有效)
  18. SmartRF_Packet_Sniffer_2.18.1软件__2.18.0软件
  19. php 读取微信对账单,扣丁学堂PHP培训简述PHP如何实现微信对账单处理
  20. undefined运算

热门文章

  1. matlab bs2rv.m,matlab遗传算法工具箱中的例子不能用?
  2. mysql 还原数据库 unknown command 'b'_mysql数据库还原出错ERROR:Unknown command ‘\\’解决手记...
  3. 博客园里输入latex公式
  4. 织梦自定义表单地区联动类型不可用的解决办法
  5. Mbatis是什么?怎么运行?
  6. MVC判断用是否登录了平台
  7. 【第四篇章-android平台MediaCodec】推断是否支持硬件解码码
  8. 同样版本的jstl,都是jstl1.2版本,有个有问题,另一个没有问题
  9. 阿里淘宝天猫单点登录项目实战(附源码)
  10. 诗与远方:无题(二十一)