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. Linux 守护进程和超级守护进程(xinetd)

    一 .Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台的守护进程来执行的 ...

  2. OI 中的 FFT

    不行啊最近备考简直变成文化狗了= =..我还脑洞大开想学俄语什么心态.. 简单地说一下FFT(来,跟我一起念,法〰法〜塔,法斯特~福铝页~圈死佛而母).. FFT本来是做信号变换用的,当然OI和信号变 ...

  3. Python字符串与数字互转,数字格式化

    # -*- coding: gbk -*- import re #将数字格式化为带三位数逗号的字符串 def formatNumber(number): numStr='%d'%number form ...

  4. total commander相关设置

    一. 中文语言包 在官方网站上提供有简体中文语言包,下面的说明以此为准.下载的语言包放至Total Commander安装目录下的Language子目录中.从菜单“Configuration”→“Op ...

  5. 【OpenStack】OpenStack系列7之Nova详解

    源码下载.安装 参考: https://github.com/yongluo2013/osf-openstack-training/blob/master/installation/openstack ...

  6. 【云计算】docker build如何支持参数化构建?

    docker 1.9.0版本之后,已经支持docker build参数化构建. docker 版本更新记录: github讨论: 参开资料: https://github.com/docker/doc ...

  7. 【SpringMVC】SpringMVC系列10之视图与视图解析器

    10.视图与视图解析器 10.1.概述     请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...

  8. 《ASP.NET1200例》<asp:DataList>分页显示图片

    aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...

  9. Counting Bits

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  10. HDU 5745 La Vie en rose (DP||模拟) 2016杭电多校联合第二场

    题目:传送门. 这是一道阅读理解题,正解是DP,实际上模拟就能做.pij+1 指的是 (pij)+1不是 pi(j+1),判断能否交换输出即可. #include <iostream> # ...