Anniversary party

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6955   Accepted: 4003

Description

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.

Input

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 N – 1 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. Input is ended with the line 
0 0 

Output

Output should contain the maximal sum of guests' ratings.

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

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 1<<30
using namespace std; int father[],dp[][],visit[],N,l,k,root; void dfs(int node){
// visit[node]=1;
for(int i=;i<=N;i++){
if(/*visit[i]==0&&*/father[i]==node){
dfs(i);
dp[node][]+=dp[i][];
dp[node][]+=max(dp[i][],dp[i][]);
}
}
} int main(){
// freopen("01.txt","r",stdin);
scanf("%d",&N);
for(int i=;i<=N;i++){
scanf("%d",&dp[i][]);//去的情况
}
while(scanf("%d%d",&l,&k)==&&l+k>){
father[l]=k;
root=k;
}
dfs(root);
printf("%d\n",max(dp[root][],dp[root][]));
return ;
}

我不会说这是TYVJ P1052 没有上司的舞会(数据都一样...)

//题意:有n个人,接下来n行是n个人的价值,再接下来n行给出l,k说的是l的上司是k,这里注意l与k是不能同时出现的
//思路:用dp数据来记录价值,开数组用下标记录去或者不去

则状态转移方程为:

DP[i][1] += DP[j][0],

DP[i][0] += max{DP[j][0],DP[j][1]};其中j为i的孩子节点。

这样,从根节点r进行dfs,最后结果为max{DP[r][0],DP[r][1]

POJ 2342 Label:树形dp的更多相关文章

  1. Anniversary party POJ - 2342 (树形DP)

    题目链接:  POJ - 2342 题目大意:给你n个人,然后每个人的重要性,以及两个人之间的附属关系,当上属选择的时候,他的下属不能选择,只要是两个人不互相冲突即可.然后问你以最高领导为起始点的关系 ...

  2. POJ 2342 (树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3863   Accepted: 2172 ...

  3. [POJ 1155] TELE (树形dp)

    题目链接:http://poj.org/problem?id=1155 题目大意:电视台要广播电视节目,要经过中转机构,到观众.从电视台到中转商到观众是一个树形结构,经过一条边需要支付成本.现在给你每 ...

  4. Apple Tree POJ - 2486 (树形dp)

    题目链接: D - 树形dp  POJ - 2486 题目大意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走V步,最多能遍历到的权值 学习网址:https://blog.c ...

  5. POJ 3107.Godfather 树形dp

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7536   Accepted: 2659 Descrip ...

  6. POJ 3342 (树形DP)

    题意 :给出一些上下级关系,要求i和i的直接上级不能同时出现,现在选出一些人构成一个集合,问你这个集合里面的最大人数是都少,同时给出这个最大的人数的集合是否唯一. 思路:树形DP,dp[i][0],表 ...

  7. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

  8. [poj 2342]简单树dp

    题目链接:http://poj.org/problem?id=2342 dp[i][0/1]表示以i为根的子树,选或不选根,所能得到的最大rating和. 显然 dp[i][0]=∑max(dp[so ...

  9. POJ 1947Rebuilding Roads(树形DP + 01背包)

    题目链接 题意:给出一个树形结构,求P个节点的子树最少要去掉几条边 分析:DP[root][j] 表示 以第 root 个为根节点, 包含j 个节点需要去掉几条边.那么对于 root 这个根节点来说, ...

随机推荐

  1. bwa的使用方法

    bwa的使用需要两中输入文件:    Reference genome data(fasta格式 .fa, .fasta, .fna)    Short reads data (fastaq格式 .f ...

  2. sublime text多文件夹查找关键字

    Ctrl+shift+F 快捷键在文件夹内查找,与普通编辑器不同的地方是sublime允许添加多个文件夹进行查找 转自:http://www.douban.com/note/362268947/

  3. jdk新特性

    自动拆装箱子: import org.junit.Test; public class Demo { /* * 自动拆装箱 * */ @Test public void ZhuangXiang() { ...

  4. cas单点登录用户名为中文的解决办法

    当用户名为中文时,登录后返回的用户名乱码.解决这个问题只需要在客户端的CAS Validation Filter中添加下配置就行了. <init-param> <param-name ...

  5. sqltext的参数化处理

    说到sql的参数化处理,我也是醉了,因为sql引擎真的是一个无比强大的系统,我们平时做系统的时候都会加上缓存,我想如果没有缓存,就不会有什么 大网站能跑的起来,而且大公司一般会在一个东西上做的比较用心 ...

  6. 【python】一个简单的贪婪爬虫

    这个爬虫的作用是,对于一个给定的url,查找页面里面所有的url连接并依次贪婪爬取 主要需要注意的地方: 1.lxml.html.iterlinks()  可以实现对页面所有url的查找 2.获取页面 ...

  7. VirtualBox 虚拟 Ubuntu 的一些感想

    之前有说过最近在学习Shell,公司及家里的电脑目前都是Windows系统,进行shell学习主要是用一些模拟linux环境的软件,比如banbun.cygwin banbun的功能很强大,集成了oh ...

  8. HDU 5744 Keep On Movin (贪心) 2016杭电多校联合第二场

    题目:传送门. 如果每个字符出现次数都是偶数, 那么答案显然就是所有数的和. 对于奇数部分, 显然需要把其他字符均匀分配给这写奇数字符. 随便计算下就好了. #include <iostream ...

  9. 一个程序中关于多个osgGA::GUIEventHandler同时存在的问题

    平时使用GUIEventHandler不太注意handle()函数的返回值,觉得返回true或者false都无所谓,其实不然. 我遇到的问题是程序中一个节点添加了GUIEventHandler对象pi ...

  10. Struts文件上传allowedTypes问题,烦人的“允许上传的文件类型”

    Struts的文件上传问题,相信很多人都会使用allowedTypes参数来配置允许上传的文件类型,如下. <param name="allowedTypes"> im ...