当前位置:网站首页>Anniversary party
Anniversary party
2022-06-28 08:31: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
The question : Yes n personal , Next n The row corresponds to everyone's happiness score , Yes n Group correspondence , If someone goes , Then its leaders cannot go , If this person doesn't go , Its leaders can go or not , On the contrary, the same is true , Ask how to arrange the highest happiness score to output the highest score .
Ideas : The question is dp problem ,dp[i][0] It means the first one i I will not go ,dp[i][1] It means the first one i Go alone ., If the boss boss Go to the dance ,i I have no choice but not to go , If the boss boss If not ,i You don't have to go , So the state transfer equation is :
dp[boss][1]=dp[boss][1]+dp[i][0];
dp[boss][0]=dp[boss][0]+max(dp[i][0],dp[i][1])
Finally, compare the maximum output of that value , The code is as follows :
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n; //dp[i][0] The first i If you don't go, you can get the maximum ,dp[i][1] The first i The maximum value that a person can go to
int f[6020],dp[6020][2],book[6020];
void dfs(int boss)
{
book[boss]=1; // This node has been used by the boss
for(int i=1;i<=n;i++)
{
if(!book[i]&&f[i]==boss) // look for boss Subordinates of
{
dfs(i); // Recursion by subordinates
dp[boss][1]=dp[boss][1]+dp[i][0]; // If the boss boss Go to the dance ,i I have no choice but not to go
dp[boss][0]=dp[boss][0]+max(dp[i][0],dp[i][1]); // If the boss boss If not ,i You don't have to go , So take the maximum
}
}
}
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]);// Everyone's happiness when everyone goes
while(~scanf("%d%d",&x,&y)&&(x||y))
{
f[x]=y; //x My boss is y
boss=y; // Record any boss
}
dfs(boss); // Find the maximum value by recursion
printf("%d\n",max(dp[boss][0],dp[boss][1]));// from boss Start recursion
}
return 0;
}
边栏推荐
- Super Jumping! Jumping! Jumping!
- Why MySQL cannot insert Chinese data in CMD
- AWS builds a virtual infrastructure including servers and networks (2)
- [learning notes] matroid
- After installing NRM, the internal/validators js:124 throw new ERR_ INVALID_ ARG_ TYPE(name, ‘string‘, value)
- [go ~ 0 to 1] on the first day, June 24, variables, conditional judgment cycle statement
- FatMouse and Cheese
- The RAC cannot connect to the database normally after modifying the scan IP. The ora-12514 problem is handled
- Kali installation configuration
- Redis deployment under Linux & redis startup
猜你喜欢

Why MySQL cannot insert Chinese data in CMD
![[learning notes] matroid](/img/e3/4e003f5d89752306ea901c70230deb.png)
[learning notes] matroid

设置cmd的编码为utf-8

Unity gets the coordinate point in front of the current object at a certain angle and distance

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week

微内核Zephyr获众多厂家支持!
![[learning notes] shortest path + spanning tree](/img/38/42b7e321389e3a47304fca5c37a2cf.png)
[learning notes] shortest path + spanning tree

B_QuRT_User_Guide(26)

爱分析发布《2022爱分析 · IT运维厂商全景报告》 安超云强势入选!

利尔达低代码数据大屏,铲平数据应用开发门槛
随机推荐
Cloudcompare & PCL point cloud SVD decomposition
B_QuRT_User_Guide(27)
JS rounding tips
Doris学习笔记之介绍、编译安装与部署
Login common test case
Redis deployment under Linux & redis startup
B_ QuRT_ User_ Guide(28)
CloudCompare&PCL 点云裁剪(基于封闭曲面或多边形)
Basic twelve style classes for duilib
块级元素上下左右居中的两个小技巧
[go ~ 0 to 1] the third day June 27 slice, map and function
Kali Notes(1)
ROS notes (09) - query and setting of parameters
webrtc优势与模块拆分
B_QuRT_User_Guide(28)
A - 深海探险
城联优品向英德捐赠抗洪救灾爱心物资
js运算符的优先级
The 6th smart home Asia 2022 will be held in Shanghai in October
FatMouse and Cheese