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 ...
随机推荐
- js动态更换img的src问题
在本地开发测试过程中,通过js动态更换img的src没有问题,图片正常切换,但是放在服务器上后测试发现,图片不显示,解决方法为:在对应onclick事件执行切换图片的js函数后加上一个return f ...
- Php—AJAX跨域问题
<?php /** * ajax proxy * ajax跨域解决办法 * @author suconghou <suconghou@126.com> * @version v1. ...
- JAVA 员工管理系统(用抽象类实现),简易版。
package Demo513; /* 定义一个Employee类,该类包含: private 成员变量name,number,birthday,其中birthday为MyDate类的对象: abst ...
- linux touch和vi建立的文件是什么文件类型的
都是acsii类型的文本文档,但是也可以建立其他格式的,比如vi newFile.c(c是c语言动态链接库格式)
- mitmweb的使用
安装mitmproxy时带有mitmweb,可直接在命令行输入命令:mitmweb 此时可打开web界面.
- UNITY_MATRIX_MVP和UnityObjectToClipPos
在unity5.6以上版本中,shader中的UNITY_MATRIX_MVP将会被UnityObjectToClipPos替代,以后我们在写顶点函数时就是这样的 v2f vert(appdata v ...
- {Linux} boot仅剩余XX字节
1. 查看已安装的linux-image各版本 dpkg --get-selections |grep linux-image 2. 查看我们当前使用的是哪一个版本: uname -a 3. ...
- SQLServer 2012 报表服务部署配置(2)
2.当系统打开"SQL Server安装中心",则说明我们可以开始正常的安装SQL Server 2012,可以通过"计划"."安装".&q ...
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- 将SQL2008升级为SQL2008 r2
我的SQL2008版本信息 Microsoft SQL Server Management Studio 10.0.1600.22 ((SQL_PreRelease).080709-1414 ...