当前位置:网站首页>Anniversary party
Anniversary party
2022-06-28 08:10:00 【Angeliaaa】
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
题意:有n个人,接下来n行对应每个人的欢乐评分,有n组对应关系,如果某个人去了,那么它的领导就不能去,如果这个人不去,它的领导去不去都行,反之同理,问怎样安排欢乐评分最高输出这个评分最高的值。
思路:这个题是dp问题,dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去.,如果上司boss去参加舞会的话,i只能不去了,如果上司boss不去的话,i可去可不去,所以状态转移方程为:
dp[boss][1]=dp[boss][1]+dp[i][0];
dp[boss][0]=dp[boss][0]+max(dp[i][0],dp[i][1])
最后比较一下那个值最大输出即可,代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n; //dp[i][0] 第i个人不去的话能取得最大值,dp[i][1]第i个人去的话能去的最大值
int f[6020],dp[6020][2],book[6020];
void dfs(int boss)
{
book[boss]=1; //这个节点为上司时已经用过了
for(int i=1;i<=n;i++)
{
if(!book[i]&&f[i]==boss) //找boss的下属
{
dfs(i); //由下属递归下去
dp[boss][1]=dp[boss][1]+dp[i][0]; //如果上司boss去参加舞会的话,i只能不去了
dp[boss][0]=dp[boss][0]+max(dp[i][0],dp[i][1]); //如果上司boss不去的话,i可去可不去,因此要取最大值
}
}
}
int main()
{
while(~scanf("%d",&n))
{
int i,x,y,boss=0;
memset(book,0,sizeof(book));
memset(dp,0,sizeof(dp));
memset(f,0,sizeof(f));
for(i=1; i<=n; i++)
scanf("%d",&dp[i][1]);//每一个人都去的时候每个人的欢乐度
while(~scanf("%d%d",&x,&y)&&(x||y))
{
f[x]=y; //x的上司是y
boss=y; //记录任意一个上司
}
dfs(boss); //通过递归求最大值
printf("%d\n",max(dp[boss][0],dp[boss][1]));//由boss开始递归
}
return 0;
}
边栏推荐
- Redis cluster deployment and application scenarios
- The micro kernel zephyr is supported by many manufacturers!
- 图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换
- B_QuRT_User_Guide(27)
- 设置cmd的编码为utf-8
- Prometheus monitoring (I)
- RAC enable archive log
- MySQL implements transaction persistence using redo logs
- Image translation /transformer:ittr: unpaired image to image translation with transformers
- 解决npm ERR! Unexpected end of JSON input while parsing near问题
猜你喜欢

ROS notes (08) - definition and use of service data

B_ QuRT_ User_ Guide(27)

Children's unit of 2022 Paris fashion week ended successfully at Wuhan station on June 19

【学习笔记】最短路 +生成树

Solution: selenium common. exceptions. WebDriverException: Message: ‘chromedriver‘ execu

图像翻译:UVCGAN: UNET VISION TRANSFORMER CYCLE-CONSISTENT GAN FOR UNPAIRED IMAGE-TO-IMAGE TRANSLATION

sql主從複制搭建

新唐NUC980使用记录:自制开发板(基于NUC980DK61YC)

In flood fighting and disaster relief, the city donated 100000 yuan of love materials to help Yingde

sql主从复制搭建
随机推荐
开户券商怎么选择?网上开户是否安全么?
npm清理缓存
Vagrant installation
Two tips for block level elements
ROS 笔记(08)— 服务数据的定义与使用
Redis master-slave structure and application scenarios
11grac turn off archive log
SQL master-slave replication setup
解决npm ERR! Unexpected end of JSON input while parsing near问题
About RAC modifying scan IP
Unity - use of API related to Pico development input system ---c
Redis persistence problem and final solution
Airflow2.1.1 summary of the pits stepped on in actual combat!!
Oracle view tablespace usage
Almost Union-Find(带权并查集)
Activity隐式跳转
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
Redis cerebral fissure
MySQL tablespace parsing
js取整的小技巧