codevs 1380 没有上司的舞会|树形动规
来源:互联网 发布:ubuntu 16.04 lrzsz 编辑:程序博客网 时间:2024/06/05 10:35
**
题目描述 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
**
**
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
**
数据范围及提示 Data Size & Hint
**
各个测试点 :1s
很典型的树形动规
dp[i][0]表示该点(人)不选取(不参加舞会)的情况下儿子(下属)所能得到的最大值(欢乐值)
dp[i][1]按照上面的对应下~
转移方程看代码吧……(打了一会发现找不到求和符号而且子节点不好表示……)(虽然觉得我的题解没人看QAQ)
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=6010;struct data {int to,next;};int head[maxn],cnt;data e[maxn];inline void insert(int u,int v){cnt++;e[cnt].to=v;e[cnt].next=head[u];head[u]=cnt;}int in[maxn];int n;int w[maxn];int dp[maxn][2];inline int DP(int x,int y){ if(dp[x][y])return dp[x][y]; if(y==1) { dp[x][1]=w[x]; for(int i=head[x];i;i=e[i].next) dp[x][y]+=DP(e[i].to,0); } else for(int i=head[x];i;i=e[i].next) dp[x][y]+=max(DP(e[i].to,0),DP(e[i].to,1)); return dp[x][y];}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&w[i]); } for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&v,&u); insert(u,v); in[v]++; } int i,ans=0; for(i=1;i<=n;i++) if(!in[i]) ans+=max(DP(i,0),DP(i,1)); printf("%d",ans); return 0;}
0 0
- codevs 1380 没有上司的舞会|树形动规
- codevs 1380 没有上司的舞会(树形dp)
- Codevs 1380 没有上司的舞会 [树形dp]
- codevs 1380 没有上司的舞会 树形DP
- 【codevs 1380】没有上司的舞会(树形dp)
- CodeVS 1380 没有上司的舞会(树形DP)
- CodeVS.1380 没有上司的舞会 (树形DP)
- CODEVS 1380 没有上司的舞会
- CODEVS 1380没有上司的舞会
- 【codevs 1380】没有上司的舞会
- CODEVS 1380 没有上司的舞会
- codevs 1380 没有上司的舞会
- Codevs 1380 没有上司的舞会
- codevs 没有上司的舞会
- 1380 没有上司的舞会 树形dp
- 洛谷 P1352 TYVJ P1052 CODEVS 1380 没有上司的舞会
- codevs 1380 没有上司的舞会 题解报告
- codevs 1380 没有上司的舞会 DP 解题报告
- 3.3.6 使用difflib.context_diff函数
- iphone media query
- vim显示行号、语法高亮、自动缩进的设置
- java中的for循环
- Eclipse中跟踪调试Android源代码
- codevs 1380 没有上司的舞会|树形动规
- kd-tree
- okhttp maven下载地址
- 用 C# 实现 HTTP 协议多线程下载文件
- IDEA Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executin
- Android代码优化小技巧总结
- HTML5试题
- 对多线程通信的优化
- 选择控件 UISegmentedController