POJ:2342-Anniversary party(树形dp入门题目)
传送门:http://poj.org/problem?id=2342
Anniversary party
Time Limit: 1000MS Memory Limit: 65536K
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
- 题意很简单就是公司要举办一个party,但是每个人都不想和自己的直属上司一同在party里面,每个人如果参见party有一个欢乐值,要求你计划安排要举办的party欢乐值最大。
- 其实就是一个树形dp,dp[i][j]代表第i个人是否来(j为1代表来,j为0代表不来),然后就是状态转移方程:dp[root][0] = max(dp[pre_root][1],dp[pre_root][0];
dp[root][1] += dp[pre_root][0]; - 主要考察的就是一个建树的过程,以及寻找根节点,关于dp的部分还是很简单的。
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 6010;
int dp[maxn][3],n;
bool vis[maxn];//用来查找根节点
vector<int> ve[maxn];//用来存树
void init()
{
for(int i=0;i<=n+1;i++)
ve[i].clear();
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
scanf("%d",&dp[i][1]);
int S,E;
while(scanf("%d%d",&S,&E) && S+E)
{
ve[E].push_back(S);
vis[S] = true;
}
}
void dfs(int x)
{
for(int i=0;i<ve[x].size();i++)
{
int temp = ve[x][i];
dfs(temp);
//状态转移方程
dp[x][0] += max(dp[temp][0],dp[temp][1]);
dp[x][1] += dp[temp][0];
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
init();
for(int i=1;i<=n;i++)
if(!vis[i])
{
dfs(i);
printf("%d\n",max(dp[i][0],dp[i][1]));
break;
}
}
return 0;
}
POJ:2342-Anniversary party(树形dp入门题目)的更多相关文章
- poj 2342 Anniversary party 树形DP入门
题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...
- POJ 2342 - Anniversary party - [树形DP]
题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...
- POJ 2342 Anniversary party 树形DP基础题
题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- [poj2342]Anniversary party树形dp入门
题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max ...
- POJ 2342 Anniversary party (树dp)
题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...
- 树形DP入门题目推荐以及解析
关于树形DP几道入门题目 今天恶补树形DP,感觉海星. 其实挺简单的. 介绍几道例题,我会的. 1.洛谷P1352 没有上司的舞会 我的一篇题解 我们可以考虑每一个节点都是有两种情况. 一个是被邀请: ...
- POJ 1463 Strategic game(树形DP入门)
题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...
- poj 2342 && hdu 1520 树形dp
题意:有n个人,接下来n行是n个人的价值,再接下来n行给出l,k说的是l的上司是k,这里注意l与k是不能同时出现的 链接:点我 dp[i][1] += dp[j][0], dp[i][0] += ma ...
随机推荐
- 转 Oracle 11g Rman – 08317错误
在一次帮助客户解决归档满的过程中遭遇了此错误. 客户是新上线系统,11g版本.设置了归档清除脚本(脚本参考:http://www.ludatou.com/?p=766),结果发现以往没问题的脚本在此刻 ...
- 开源分布式Job系统,调度与业务分离-如何创建周期性的HttpJob任务
项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对Hangfire开发的一个组件,该组件和Hangfire本身是独立的.可以独立 ...
- 写TXT文件
#region 写日志 private static void writelog(string strwrite) { string strPath = "d:/log.txt"; ...
- sql常用操作(一)
sql(structured query language,结构化查询语言)语言:和数据库交互的语言,进行数据库管理的语言. 1.1 sql语句的作用:说白了就是增删改查 管理数据库 管理表 管理数据 ...
- DetachedCriteria的简单使用
一. DetachedCriteria使得hibernate能够对查询条件进行面向对象的方式来组装.其创建方式有两种: 1.1直接用class创建:DetachedCriteria criteria ...
- Unity3d通过脚本生成apk
参考链接:http://www.jianshu.com/p/a9261113b4ac 照着链接的方法并没有正确生成APK,IPA没有测试过,不过大致的方法是正确的,修改如下: Environment. ...
- sqlserver中计算某个特殊字符在字符串中出现的位置
-- ============================================= -- Author: Evan -- Create date: 2018年3月15日10:: -- D ...
- python爬虫之路——无头浏览器初识及简单例子
from selenium import webdriver url='https://www.jianshu.com/p/a64529b4ccf3' def get_info(url): inclu ...
- HTML之基本语法(链接标签、路径的介绍和使用)
一.链接标签 语法:<a href="目标地址">这个标签上展示的内容</a> 作用:可以实现在当前页面跳转到新页面的操作 属性 1.target这个属性可 ...
- Cookie中存放数据l加密解密的算法
public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...