Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6635   Accepted: 3827

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 <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = ;
int dp[N][]; //dp[i][0]表示当前i点不选 1表示选
int father[N],vis[N];
int n;
void creat(int m)
{
vis[m]=;
for(int i=;i<=n;i++)
{
if(vis[i]== && father[i]==m)
{
creat(i); //cout<<m<<endl;
dp[m][]+=max(dp[i][],dp[i][]);//m不去,取i去或不去的最大值
dp[m][]+=dp[i][];//m去,则i必不能去
}
}
}
int main()
{
int i;
while(~scanf("%d",&n))
{
Del(dp,);Del(father,);
Del(vis,);
for(i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
int f,c,root;
root = ;//记录父结点
bool beg = ;
while (scanf("%d %d",&c,&f),c||f)
{
father[c] = f;
if( root == c || beg )
{
root = f;
}
}
while(father[root])//查找父结点
root=father[root];
creat(root);
int imax=max(dp[root][],dp[root][]);
printf("%d\n",imax);
}
return ;
}

POJ2342 Anniversary party(动态规划)(树形DP)的更多相关文章

  1. poj2342 Anniversary party (树形dp)

    poj2342 Anniversary party (树形dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9128   ...

  2. poj2342 Anniversary party【树形dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316097.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  3. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  4. 动态规划——树形dp

    动态规划作为一种求解最优方案的思想,和递归.二分.贪心等基础的思想一样,其实都融入到了很多数论.图论.数据结构等具体的算法当中,那么这篇文章,我们就讨论将图论中的树结构和动态规划的结合——树形dp. ...

  5. Anniversary party (树形DP)

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  6. hdu1520 Anniversary party 简单树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...

  7. HDU1520:Anniversary party(树形dp第一发)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1520 一个公司去参加宴会,要求去的人不能有直接领导关系,给出每一个人的欢乐值,和L K代表K是L的直接领导 ...

  8. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  9. hdu 1520 Anniversary party(入门树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6926   Accepted: 3985 ...

随机推荐

  1. Windows查看进程CMD命令和终止进程CMD命令

    将小米路由器3刷机成openwrt的方法,请参考上篇文章< 家庭宽带多运营商接入方案>这里介绍怎么在已经刷成openwrt系统的小米路由器3上安装私有云nextcloud openwrt开 ...

  2. 【NOIP模拟赛】 permutation 数学(打表)

    biubiu~~~ 这道题卡读题卡得很死......首先他告诉我们读循环的时候要顺着圈读,然后又说这个圈在数列上要以最大数开始读,而且以这样的循环的首数排序,得到的序列与原序列一样那么他就是可行序列, ...

  3. 【BZOJ3887】【Usaco2015 Jan】Grass Cownoisseur Tarjan+Spfa

    我们可以看出这个东西可以缩点成DAG,因为我们在所称的点里用特技的话,要么没用,要么削弱自己对点的收割能力与边的联通权,所以我们缩完点之后在图上枚举反向的变,因为我们只可能反向一条边,而且我们知道在这 ...

  4. linux 学习好资源

    Linux-Wiki.cn http://linux-wiki.cn/wiki/zh-hans/Linux%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84    Linux目录 ...

  5. 纯手工 CheckboxTree 实现

    数据结构及页面显示格式: INSERT INTO AS_CombRules VALUES('', '', '', '', '', '', '') 实现 CheckboxTree 功能: html代码: ...

  6. ES6学习笔记(五)—— 编程风格

    1. 块级作用域 let 取代 var —— let 只在声明的代码块内有效,而且不存在变量提升的效用 const 取代 let —— const 比较符合函数式编程的思想,运算不改变值,只是新建值: ...

  7. 知问前端——日历UI(一)

    日历(datepicker)UI,可以让用户更加直观的.更加方便的输入日期,并且还考虑不同国家的语言限制,包括汉语. 调用datepicker()方法 $('#date').datepicker(); ...

  8. 01-UIDynamic简单演练demo源代码

    源代码下载: 01-简单演练.zip72.0 KB // //  ViewController.m //  01.简单演练 // //  Created by apple on 13-12-24. / ...

  9. Linux内核学习之中断 中断本质【转】

    转自:http://www.linuxidc.com/Linux/2011-11/47657.htm [中断概述] 中断本质上是一种特殊的电信号,由硬件设备发向处理器.异常和中断的不同是异常在产生时必 ...

  10. Flex slider使用方法

    1.首先在页面head部位载入jquery库文件和Flexslider插件,以及Flexslider所需的基本css样式文件. <link rel="stylesheet"  ...