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 常用软件和服务的配置网站

    http://www.linuxidc.com/Linux/2012-11/73862.htm Linux zabbix的安装和配置

  2. 什么是mixin

    转自:http://guangboo.org/2013/01/28/python-mixin-programming http://en.wikipedia.org/wiki/Mixin http:/ ...

  3. Intersection of Two Arrays | & ||

    Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...

  4. poj 1833

    http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排 ...

  5. HDU 5793 A Boring Question (逆元+快速幂+费马小定理) ---2016杭电多校联合第六场

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 借助magicwindow sdk plugin快速集成sdk

    到目前为止,Android Studio已经是开发原生Android App的主流IDE,它是由Google官方设计并基于JetBrains的IntelliJ IDEA.我们魔窗开发的sdk也是使用此 ...

  7. Excel 关于新建xls文件 新建sheet 合并sheet的VBA操作代码

    Sub 合并一个文件夹下全部xls文件中sheet到一个xls的sheet()workDir = ThisWorkbook.Path '当前xls文件所在的目录绝对路径'MsgBox workDir, ...

  8. 手机 无法转移到SD卡 Andriod 导出应用程序

    今天下载了一个应用Meridian,因为是直接从google商店里面装的,因此无法找到SDK,需要从新找到导出方式: 豌豆荚==> 软件和游戏管理==>输入Meridian搜素==> ...

  9. Zabbix discoverer processes more than 75% busy

    [root@86 ~]# grep -n "StartDiscoverers" /usr/local/zabbix/etc/zabbix_server.conf 176:### O ...

  10. navicat使用跳板机连接数据库-ssh

    1. 目标数据库的域名/IP,端口,用户名,密码:如图1 2. 这时候不要点OK!选择SSH这个tab 3. 选中User SSH Tunnel:填写跳板机域名/IP,用户名,密码(注意:端口22不要 ...