洛谷——P1352 没有上司的舞会
2017-05-06 09:01
183 查看
https://www.luogu.org/problem/show?pid=1352#sub
题目描述
某大学有N个职员,编号为1~N。他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司。现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但是呢,如果某个职员的上司来参加舞会了,那么这个职员就无论如何也不肯来参加舞会了。所以,请你编程计算,邀请哪些职员可以使快乐指数最大,求最大的快乐指数。
输入输出格式
输入格式:
第一行一个整数N。(1<=N<=6000)
接下来N行,第i+1行表示i号职员的快乐指数Ri。(-128<=Ri<=127)
接下来N-1行,每行输入一对整数L,K。表示K是L的直接上司。
最后一行输入0 0
输出格式:
输出最大的快乐指数。
输入输出样例
输入样例#1:7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0输出样例#1:
5
用f[i][0]表示选i,f[i][1]表示不选
然后如果选了i,那么连出去的点一定不选——f[i][0]+=f[v][1]
否则连出去的点可能选也可能不选——f[i][1]+=max(f[v][0],f[v][1])
#include <algorithm> #include <cstdio> #include <vector> using namespace std; const int N(6015); vector<int>vec ; int n,x,y,w ,root; int dad ,f [2]; void DFS(int x) { f[x][0]=w[x]; for(int i=0;i<vec[x].size();i++) { DFS(vec[x][i]); f[x][1]+=max(f[vec[x][i]][0],f[vec[x][i]][1]); f[x][0]+=f[vec[x][i]][1]; } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",w+i); while(scanf("%d%d",&x,&y)&&x&&y) { dad[x]=y; vec[y].push_back(x); } for(root=1;root<=n;++root) if(!dad[root]) break; DFS(root); printf("%d",max(f[root][1],f[root][0])); return 0; }
相关文章推荐
- POJ 2342 Anniversary party 洛谷 P1352 没有上司的舞会
- 洛谷 P1352 没有上司的舞会
- 洛谷 p1352 没有上司的舞会 题解
- 洛谷 p1352 没有上司的舞会 题解
- [P1352]没有上司的舞会
- 洛谷 P1352 TYVJ P1052 CODEVS 1380 没有上司的舞会
- 【洛谷 P1352】【codevs1380】没有上司的舞会
- P1352 没有上司的舞会
- 洛谷 P1352 没有上司的舞会
- P1352没有上司的舞会
- codevs 1380 没有上司的舞会 - 树形动态规划
- codevs 1380 没有上司的舞会(树形dp)
- 没有上司的舞会
- 洛谷P1352 没有上司的舞会
- 没有上司的舞会
- POJ 2342 Anniversiry Party(TYVJ1052 没有上司的舞会)
- luogu P1352 没有上司的舞会 x
- 【洛谷1352】 没有上司的舞会
- codves :没有上司的舞会
- Codevs1380 没有上司的舞会