描述

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests’ conviviality ratings.

输入

Multiple cases, for each case:

Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:

L K

It means that the K-th employee is an immediate supervisor of the L-th employee. each case is ended with the line

0 0

输出

For each case, output should contain the maximal sum of guests’ ratings.

样例输入

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

样例输出

5

题意

每个点有一个权值,给你几个点之间的关系 L K,点L的上司是K点。
你需要选出一些点,选中的点中不能有直属的上司和下属关系,要你输出这些选中的点的和最大的值

我们从根节点出发来选取点。定义dp[i][0]表示不选中这个点时的最优解,dp[i][1]表示选中这个点时的最优解。
那么我们能得出这个点的值是由它的下属来决定的
dp[k][1]+=dp[u][0];//选择这个点,那么它的下属就不能选择
dp[k][0]+=max(dp[u][0],dp[u][1]);//不选择这个点那么它的下属可选可不选
最后我们还需要判断一下你的根节点是否要选取

#include <bits/stdc++.h>
using namespace std;
const int N=6005;
vector<int>G[N];
int vis[N],dp[N][2],fa[N];
int n,x,y;
void f(int k){vis[k]=1;int num=G[k].size();for(int i=0;i<num;i++){if(vis[G[k][i]]==0){f(G[k][i]);dp[k][1]+=dp[G[k][i]][0];dp[k][0]+=max(dp[G[k][i]][0],dp[G[k][i]][1]);}}
}
int gen(int x){if(x==fa[x]) return x;return fa[x]=gen(fa[x]);
}int main()
{while(~scanf("%d",&n)){for(int i=0;i<=n;i++) G[i].clear();memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++){dp[i][0]=0;fa[i]=i;scanf("%d",&dp[i][1]);}while(scanf("%d%d",&x,&y),x||y){G[y].push_back(x);int xx=gen(x),yy=gen(y);if(xx!=yy){fa[xx]=yy;}}int tt;for(int i=1;i<=n;i++){if(fa[i]==i){tt=i;break;}}f(tt);printf("%d\n",max(dp[tt][0],dp[tt][1]));}return 0;
}

Anniversary party(最基础的树形dp)相关推荐

  1. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  2. Luogu P1122 最大子树和 树形DP

    题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明 ...

  3. HDU 1011 Starship Troopers星河战队(树形dp)

    题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即根节点是1. 每个洞穴有x只bugs,并有价值y的金子,全部消灭完一个洞穴的虫子,就可以获得这个洞穴的y个金子. 现 ...

  4. HDU - 1520 Anniversary party [树形dp]

    Anniversary party 时限:1000ms Problem Description There is going to be a party to celebrate the 80-th ...

  5. HDU 1520 Anniversary party(树形dp)

    HDU 1520 Anniversary party(树形dp) 树形dp第一题!!! 题意很清晰,思路也很明确.很容易找到根节点,即最大的boss,通过根节点向下dp. 状态转移方程: int to ...

  6. 【HDU - 1520】Anniversary party (树形dp)

    题干: There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. Th ...

  7. 树形dp瞎讲+树形dp基础题题解

    ---恢复内容开始--- 没错 咕了这么久(没有青青姐久 我又开始写博客了( ´▽`) 想了很久些什么(才没有想过呢 虽然被鄙视基础不好但还是走上了树形dp的不归路 那么 就来写写树形dp吧(dtx ...

  8. [NC15748]旅游 树形dp基础

    菜鸡第一次接触树形dp这个东西,不过这个东西还是很好理解的(可能是因为模板题吧) 个人感觉,相比线性dp,树形dp的状态转移方程更加的直观,难点主要是在"树"的结构上比较麻烦. 题 ...

  9. POJ - 2342 Anniversary party(树形dp入门)

    题目链接:点击查看 题目大意:每个人都有一个快乐值,给定一个树状的从属关系,仅当上司和下属都不在的时候这个个人的快乐值才能表现出来,问怎么样才能让整体的快乐值达到最大 题目分析:做线段树做吐了,来换换 ...

最新文章

  1. 阅读笔记: 凸包的例子(一)
  2. 大数据旅游网站Hopper获新一轮1600万美元融资
  3. U-Boot启动流程详解
  4. 切割图形_泉州泡沫景观字切割机厂家
  5. 如何用轻量级RTSP服务本地生成RTSP测试URL
  6. 用于数据输入的基本WPF窗口功能
  7. 布易般的旅途(一) 像流水一般的生活
  8. maven内置属性详细说明
  9. 利用oc门或od门实现线与_TTL,CMOS,OC门,OD门的理解
  10. 史上最详细阿里云服务器搭建网站流程(图文教程)
  11. Python3.5爬取cbooo.cn数据并且同步到mysql中
  12. 通过 InnoSetup 美化安装界面
  13. 机器学习吴恩达课程总结(一)
  14. Power Apps函数笔记
  15. Altium Designer学习笔记——布线技巧
  16. 加勒比海盗船——最优解转载问题c++
  17. 泰然金融创始人潘宝锋分析网贷行业发展动态
  18. ValueError: The truth value of a DataFrame is ambiguous. Use a.empty 解决办法。(附 if 深层理解)
  19. WPF源码控件库《Newbeecoder.UI》轮播
  20. [Unity3D学习]3D数学基础之向量

热门文章

  1. (2020.12.7)初次web前端性能优化记录
  2. CSS多行文本溢出隐藏,显示省略号
  3. 精简压缩优化 Docker 镜像几百MB
  4. USACO 2.1 健康的荷斯坦奶牛 Healthy Holsteins
  5. 二叉树 | 二叉树的深度
  6. C++进阶 必读书籍
  7. Android 进程间通信方式
  8. “芯片自由”后荣耀CEO赵明发声:发布新机,冲击高端
  9. Unity Animator入门:使用Animator和trigger参数做简单的UI动画
  10. PyQt5 QtChart-折线图