Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8028   Accepted: 4594

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

Source

解题思路:
第一个树形dp的题、、、
思路还是比较好理解的。
每个人有来与不来两种状态,当来的时候,他的直接下属就不能来。----①
当他不来的时候,他的直接下属可以来,可以不来。----②
用dp[i][1]表示编号为i的人的时候,以它为根的子树所以产生的活跃值:由①可知dp[i][1]=dp[i][1]+dp[儿子节点][0];
用dp[i][0]表示编号为i的人不来的时候,以它为根的子树所产生的活跃值:由②可知dp[i][0]=dp[i][1]+max(dp[儿子节点][0],dp[儿子节点][1]);
 
 #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define FOR(i,x,n) for(long i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAX_N 60
#define MAX_M 1005 using namespace std; struct node{
int number;
int rating;
int sonNum;
int son[];
int father;
};
node tree[];
//int visable[6005];
int dp[][];//0表示不去,1表示去 void dfs(int root){
FOR(i,,tree[root].sonNum){
dfs(tree[root].son[i]);
dp[root][]+=max(dp[tree[root].son[i]][],dp[tree[root].son[i]][]);
dp[root][]+=dp[tree[root].son[i]][];
}
} int main()
{
//freopen("input1.txt", "r", stdin);
//freopen("data.out", "w", stdout);
int n;
//memset(visable,0,sizeof(visable));
memset(dp,,sizeof(dp));
scanf("%d",&n);
FOR(i,,n+){
tree[i].father=i;
tree[i].sonNum=;
}
FOR(i,,n+){
scanf("%d",&dp[i][]);
}
int t1,t2;
FOR(i,,n){
scanf("%d %d",&t1,&t2);
if(!(t1+t2)){
break;
}
tree[t1].father=t2;
tree[t2].son[tree[t2].sonNum++]=t1;
}
int t=;
while(tree[t].father!=t){
t=tree[t].father;
}
dfs(t);
printf("%d",max(dp[t][],dp[t][]));
//fclose(stdin);
//fclose(stdout);
return ;
}

poj2342 Anniversary party的更多相关文章

  1. poj2342 Anniversary party (树形dp)

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

  2. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

  3. DP Intro - Tree POJ2342 Anniversary party

    POJ 2342 Anniversary party (树形dp 入门题) Anniversary party Time Limit: 1000MS   Memory Limit: 65536K To ...

  4. POJ2342 Anniversary party(动态规划)(树形DP)

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

  5. 树形dp poj2342 Anniversary party * 求最大价值

    Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State Univer ...

  6. poj2342 Anniversary party【树形dp】

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

  7. [poj2342]Anniversary party树形dp入门

    题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...

  8. 【poj2342】 Anniversary party

    http://poj.org/problem?id=2342 (题目链接) 题意 没有上司的舞会... Solution 树形dp入门题. dp[i][1]表示第i个节点的子树当节点i去时的最大值,d ...

  9. Anniversary party(hdu1520)(poj2342)题解

    原题地址:http://poj.org/problem?id=2342 题目大意: 上司和下属不能同时参加派对,求参加派对的最大活跃值. 关系满足一棵树,每个人都有自己的活跃值(-128~127) 求 ...

随机推荐

  1. Python常用的软件包

    下面是Python开发常用的软件包. 名称 用途 安装命令 opengl   sudo pip3 install PyOpenGL  pyqtgraph GUI图形库 sudo pip3 instal ...

  2. MySQL优化之like关键字

    1.%号不放最左边 无法使用索引了,开头是不确定的,MySQL也无法进行优化了,只能扫描表了. 2.使用覆盖索引 如果业务需要%就放开头我们也没办法,一般情况需求都是这样的,毕竟优化还是为业务服务的. ...

  3. Android的Databinding-需要使用控件id,listener以及布局有include的场景

    主的布局xml文件: <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bin ...

  4. Mysql中DDL, DML, DCL, 和TCL是什么?

    在一些公司中提交给测试团队的SQL脚本会划分为DDL.DML等,但这些概念到底是如何定义的呢? SQL(Structure Query Language)是数据库操作的的核心语言,接下来我们通过一张图 ...

  5. shell编程学习笔记(三):Shell中局部变量的使用

    现在我们看一下Shell中局部变量的使用 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script03.sh 开始编写script0 ...

  6. 【ASP.NET Core】从向 Web API 提交纯文本内容谈起

    前些时日,老周在升级“华南闲肾回收登记平台”时,为了扩展业务,尤其是允许其他开发人员在其他平台向本系统提交有关肾的介绍资料,于是就为该系统增加了几个 Web API. 其中,有关肾的介绍采用纯文本方式 ...

  7. Java密码体系结构简介:Java Cryptography Architecture (JCA) Reference Guide

    来自Java官方的文档,作备忘使用. 简介: Java平台非常强调安全性,包括语言安全,密码学,公钥基础设施,认证,安全通信和访问控制. JCA是平台的一个主要部分,包含一个“提供者”体系结构和一组用 ...

  8. SNF软件开发机器人-子系统-功能-【列表】自由排序-如何配置?

    [列表]自由排序 1.效果展示: 2.使用说明: 打开显示页面,点击开发者选项的简单配置按钮.在功能表信息中选择自由排序复选框后保存.

  9. 在Visual Studio 2013中安装Mysql for EntityFramework

    1. 安装Visual Studio 20132. 下载mysql,安装mysql.3. 下载 mysql-for-visualstudio-1.2.7.msi, 下载链接:https://cdn.m ...

  10. 在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间

    在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间