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. 【题解】HNOI2010合唱队

    我果然还是太弱了呜呜呜……洛谷P3205 区间dp:注意到一段区间最两侧的人必然是最后加入队伍的所以由此我们可以分成两种情况来讨论. 一种是最后一个加入的人是左边的,另一种是右边的.那么状态:dp[i ...

  2. [bzoj5321] [Jxoi2017]加法

    Description 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个 ...

  3. 洛谷 P1415 拆分数列 解题报告

    拆分数列 题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数. 如果有多组解,则输出使得最后一个 ...

  4. 最小k度最小生成树模板

    代码是抄的 题解是瞄的 可我想学习的心是真的嘤嘤嘤 然而 还是上传一份ioi大神的论文吧 链接:https://pan.baidu.com/s/1neIW9QeZEa0hXsUqJTjmeQ 密码:b ...

  5. MSTest DeploymentItemAttribute

    该attribute可以把指定的文件拷贝到每次运行的Out目录下,比如有一个config文件,那么用下面的命令, [TestClass] [DeploymentItem("Default.c ...

  6. vue与node模版引擎的渲染标记{{}}(双花括号)冲突

    由于之前练习koa2,直接渲染的jquery写的传统页面. 这次想偷懒,直接script引入vue,发现渲染不出data值. 渲染引擎用得是xtpl, 找了半天没有发现可以修改xtpl渲染分隔符的配置 ...

  7. maven在add dependecy时搜索不出jar包的解决办法

    一:前言 其实我一直都很头疼maven的项目管理的,因为觉得用起来还是没有那么方便的啊,不过今天我自己算是小弄了下maven项目的故那里,是一个同事在配置maven的项目,我去凑了下热闹而已,现在自己 ...

  8. 【Luogu P3834】可持久化数组(可持久化线段树)

    题目链接 可持久化线段树模板题. 这里总结一下可持久化线段树. 可持久化数据结构就是能恢复历史状态的数据结构,比如可持久化\(Trie\),并查集,平衡树. 可持久化数组是最基础的,这里通过可持久化线 ...

  9. 单选按钮 JradioButton 和复选框 JcheckBox 的使用

    package first; import javax.swing.*; import java.awt.*; import java.awt.event.*; class BRTest extend ...

  10. TensorFlow_曲线拟合

    # coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import os ...