当前位置:网站首页>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;
}
边栏推荐
- B_ QuRT_ User_ Guide(26)
- B_ QuRT_ User_ Guide(29)
- The maximum number of Rac open file descriptors, and the processing of hard check failure
- 块级元素上下左右居中的两个小技巧
- Redis uses sentinel master-slave switching. What should the program do?
- 你了解TCP协议吗(二)?
- ROS notes (09) - query and setting of parameters
- Unity 获取当前物体正前方,一定角度、距离的坐标点
- PC端隐藏滚动条
- 你了解TCP协议吗(一)?
猜你喜欢
随机推荐
Set the icon for the title section of the page
Estimation of SQL execution cost by MySQL query optimizer
B_ QuRT_ User_ Guide(29)
JS rounding tips
Why MySQL cannot insert Chinese data in CMD
How to use redis to solve concurrency problems
B_ QuRT_ User_ Guide(26)
AI首席架构师8-AICA-高翔 《深入理解和实践飞桨2.0》
Unity gets the coordinate point in front of the current object at a certain angle and distance
NPM clean cache
Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week
Buffer pool in MySQL
【js】-【DFS、BFS应用】-学习笔记
Kubernetes notes and the latest k3s installation introduction
MySQL implements transaction persistence using redo logs
MySQL row format parsing
Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation
nlp序列完全可以模拟人脑智能
【学习笔记】模拟
cuda和cudnn和tensorrt的理解