POJ 2342 Anniversary party 【树形DP】

本文探讨了一家公司的晚宴邀请问题,旨在通过避免邀请直接上下级员工的方式,使宴会的总幸福值达到最大。文章提供了详细的算法思路,包括动态规划的使用,以及具体的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意:

一个公司要举办晚宴,公司一共有n个人。每一个人在宴会上有一个幸福值,如果在宴会上碰到了自己的直接上司,那么他的幸福值就为0。所以考虑如何邀请人让宴会幸福值的和最大,输出最大的幸福值之和。

大致思路:

对于每一个人,都有来或者不来两种可能。dp[i][0/1] 代表第i个人来或者不来情况下前i个人的幸福值之和。

利用树的性质递归求每一个dp [i]。最后取一个max。

代码:

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn=1e6+7;
 7 int father[maxn];
 8 int dp[maxn][2],n;
 9 bool visit[maxn];
10 void tree_dp(int k)
11 {
12     visit[k]=false;
13     for(int i=1;i<=n;++i){
14         if(visit[i]&&father[i]==k){
15             tree_dp(i);
16             dp[k][1]+=dp[i][0];
17             dp[k][0]+=max(dp[i][0],dp[i][1]);
18         }
19     }
20 }
21 int main()
22 {
23     ios::sync_with_stdio(false);
24     //freopen("in.txt","r",stdin);
25     int f,s,root=1;
26     memset(father,0,sizeof(father));
27     memset(dp,0,sizeof(dp));
28     memset(visit,true,sizeof(visit));
29     cin>>n;
30     for(int i=1;i<=n;++i)
31         cin>>dp[i][1];
32 
33     while(cin>>s>>f||(s&&f))
34         father[s]=f;
35     while(father[root])
36         root=father[root];
37     tree_dp(root);
38     cout<<max(dp[root][0],dp[root][1])<<endl;
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/SCaryon/p/7381985.html

资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 四路20秒声光显示计分抢答器Multisim14仿真源文件+设计文档资料摘要 数字抢答器由主体电路与扩展电路组成。优先编码电路、锁存器、译码电路将参赛队的输入信号在显示器上输出;用控制电路和主持人开关启动报警电路,以上两部分组成主体电路。通过定时电路和译码电路将秒脉冲产生的信号在显示器上输出实现计时功能,构成扩展电路。经过布线、焊接、调试等工作后数字抢答器成形。关键字:开关阵列电路;触发锁存电路;解锁电路;编码电路;显示电路 一、设计目的 本设计是利用已学过的数电知识,设计的4人抢答器。(1)重温自己已学过的数电知识;(2)掌握数字集成电路的设计方法和原理;(3)通过完成该设计任务掌握实际问题的逻辑分析,学会对实际问题进行逻辑状态分配、化简;(4)掌握数字电路各部分电路与总体电路的设计、调试、模拟仿真方法。 二、整体设计 (一)设计任务与要求: 抢答器同时供4名选手或4个代表队比赛,分别用4个按钮S0 ~ S3表示。 设置一个系统清除和抢答控制开关S,该开关由主持人控制。 抢答器具有锁存与显示功能。即选手按动按钮,锁存相应的编号,并在LED数码管上显示,同时扬声器发出报警声响提示。选手抢答实行优先锁存,优先抢答选手的编号一直保持到主持人将系统清除为止。 参赛选手在设定的时间内进行抢答,抢答有效,定时器停止工作,显示器上显示选手的编号和抢答的时间,并保持到主持人将系统清除为止。 如果定时时间已到,无人抢答,本次抢答无效。 (二)设计原理与参考电路 抢答器的组成框图如下图所示。它主要由开关阵列电路、触发锁存电路、解锁电路、编码电路和显示电路等几部分组成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值