当前位置:网站首页>The maximum happiness of the party
The maximum happiness of the party
2022-07-05 23:02:00 【Bright morning light】
1、 subject
Employee information is defined as follows :
class Employee {
public int happy; // The happiness that this employee can bring
List<Employee> subordinates; // Which direct subordinates does this employee have
};
Every employee of the company meets Employee
Class description , The personnel structure of the whole company can be regarded as a standard 、 A multitree without rings . The root node of the tree is the only boss of the company . Every employee except the boss has a unique direct superior . Leaf nodes are grassroots employees without any subordinates (subordinates
The list is empty. ), In addition to grass-roots employees , Each employee has one or more direct subordinates .
The company is going to do party, You can decide which employees come to , Which employees don't come , The rules :
- If an employee comes , Then all the direct subordinates of this employee can't come ;
- The overall happiness of the queue is the accumulation of the happiness of all employees present ;
- Your goal is to maximize the overall happiness of the queue .
Given the root node of a multitree boss
, Please return the maximum happiness of the queue .
2、 analysis
Recursive routines using binary trees :
① The goal is : With X Get the maximum for the whole tree with roots happy value
② possibility :
1)X What happened here : hypothesis X Your direct subordinate is a,b,c, So happy value = X.happy + a No way max + b No way max + c No way max;
2)X The situation of not coming : Happiness value = 0 + max{a Come on ,a Not coming } + max{b Come on ,b Not coming } + max{c Come on ,c Not coming }
You can get , The information that needs to be obtained from the left and right subtrees is :① The largest node does not come happy value ;② The largest number of nodes happy value .
3、 Realization
C++ edition
/************************************************************************* > File Name: 042. The greatest pleasure of the party .cpp > Author: Maureen > Mail: [email protected] > Created Time: Two 7/ 5 14:59:05 2022 ************************************************************************/
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
class Employee {
public:
int happy;
vector<Employee*> subordinates;
Employee(int h) : happy(h) {
}
};
// The current node is called cur
// Parameters up Express cur Whether your superior will come
// The meaning of function :
// up by true, Express cur The superior of determines the situation of coming ,cur The whole tree can provide the largest happy value
// up by false, Express cur Your superiors are not sure about the situation ,cur The whole tree can provide the largest happy value
int process1(Employee *cur, bool up) {
if (up) {
int ans = 0;
for (Employee *next : cur->subordinates) {
ans += process1(next, false);
}
return ans;
} else {
int p1 = cur->happy;
int p2 = 0;
for (Employee *next : cur->subordinates) {
p1 += process1(next, true);
p2 += process1(next, false);
}
return max(p1, p2);
}
}
int maxHappy1(Employee *boss) {
if (boss == nullptr)
return 0;
return process1(boss, false);
}
// Method 2 :
class Info {
public:
int no; // The root node does not come to the largest happy value
int yes; // The root node is the largest happy value
Info (int n, int y) : no(n), yes(y) {
}
};
Info *process(Employee *x) {
if (x == nullptr) {
return new Info(0, 0);
}
int no = 0;
int yes = x->happy;
for (Employee *next : x->subordinates) {
Info *nextInfo = process(next);
//x When you don't come , Children can come or not
no += max(nextInfo->no, nextInfo->yes);
//x when , Children cannot come
yes += nextInfo->no;
}
return new Info(no, yes);
}
int maxHappy2(Employee *boss) {
Info *allInfo = process(boss);
return max(allInfo->no, allInfo->yes);
}
//for test
void generateNexts(Employee *e, int level, int maxLevel, int maxNexts, int maxHappy) {
if (level > maxLevel) return ;
int nextsSize = rand() % (maxNexts + 1);
for (int i = 0; i < nextsSize; i++) {
Employee *next = new Employee(rand() % (maxHappy + 1));
e->subordinates.push_back(next);
generateNexts(next, level + 1, maxLevel, maxNexts, maxHappy);
}
}
Employee *generateBoss(int maxLevel, int maxNexts, int maxHappy) {
if ((rand() % 100 / (double)101) < 0.02) return nullptr;
Employee *boss = new Employee(rand() % (maxHappy + 1));
generateNexts(boss, 1, maxLevel, maxNexts, maxHappy);
return boss;
}
int main() {
srand(time(0));
int maxLevel = 4;
int maxNexts = 7;
int maxHappy = 100;
int testTimes = 100000;
for (int i = 0; i < testTimes + 1; i++) {
Employee *boss = generateBoss(maxLevel, maxNexts, maxHappy);
if (maxHappy1(boss) != maxHappy2(boss)) {
cout << "Failed!" << endl;
return 0;
}
if (i && i % 1000 == 0) cout << i << " cases passed!" << endl;
}
cout << "Success!" << endl;
return 0;
}
Java edition
import java.util.ArrayList;
import java.util.List;
public class MaxHappy {
public static class Employee {
public int happy;
public List<Employee> nexts;
public Employee(int h) {
happy = h;
nexts = new ArrayList<>();
}
}
// Method 1 :
public static int maxHappy1(Employee boss) {
if (boss == null) {
return 0;
}
return process1(boss, false);
}
// The current node is called cur,
// up Express cur Whether your superior will come ,
// This function means :
// If up by true, It means that cur The superior has decided to come , Under the circumstances ,cur What is the maximum happiness value that the whole tree can provide ?
// If up by false, It means that cur The superior has decided not to come , Under the circumstances ,cur What is the maximum happiness value that the whole tree can provide ?
public static int process1(Employee cur, boolean up) {
if (up) {
// If cur If your superior comes ,cur No choice , Can only not come
int ans = 0;
for (Employee next : cur.nexts) {
ans += process1(next, false);
}
return ans;
} else {
// If cur If your superior doesn't come ,cur You can choose , You can come or not
int p1 = cur.happy;
int p2 = 0;
for (Employee next : cur.nexts) {
p1 += process1(next, true);
p2 += process1(next, false);
}
return Math.max(p1, p2);
}
}
// Method 2 :
public static int maxHappy2(Employee head) {
Info allInfo = process(head);
return Math.max(allInfo.no, allInfo.yes); // The biggest benefit from the biggest boss , The biggest benefit of not coming to the biggest boss
}
public static class Info {
public int no; // The maximum when the root node does not come happy value
public int yes;// The maximum when the root node comes happy value
public Info(int n, int y) {
no = n;
yes = y;
}
}
public static Info process(Employee x) {
if (x == null) {
return new Info(0, 0); // Happiness value is 0
}
int no = 0;
int yes = x.happy;
for (Employee next : x.nexts) {
Info nextInfo = process(next);
no += Math.max(nextInfo.no, nextInfo.yes); //x When you don't come , Children can come , Or not , Find the maximum of both
yes += nextInfo.no;// Children cannot come
}
return new Info(no, yes);
}
// for test
public static Employee genarateBoss(int maxLevel, int maxNexts, int maxHappy) {
if (Math.random() < 0.02) {
return null;
}
Employee boss = new Employee((int) (Math.random() * (maxHappy + 1)));
genarateNexts(boss, 1, maxLevel, maxNexts, maxHappy);
return boss;
}
// for test
public static void genarateNexts(Employee e, int level, int maxLevel, int maxNexts, int maxHappy) {
if (level > maxLevel) {
return;
}
int nextsSize = (int) (Math.random() * (maxNexts + 1));
for (int i = 0; i < nextsSize; i++) {
Employee next = new Employee((int) (Math.random() * (maxHappy + 1)));
e.nexts.add(next);
genarateNexts(next, level + 1, maxLevel, maxNexts, maxHappy);
}
}
public static void main(String[] args) {
int maxLevel = 4;
int maxNexts = 7;
int maxHappy = 100;
int testTimes = 100000;
for (int i = 0; i < testTimes; i++) {
Employee boss = genarateBoss(maxLevel, maxNexts, maxHappy);
if (maxHappy1(boss) != maxHappy2(boss)) {
System.out.println("Oops!");
}
}
System.out.println("finish!");
}
}
边栏推荐
- Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Hj16 shopping list
- [untitled]
- Solve the problem of "no input file specified" when ThinkPHP starts
- Getting started stm32--gpio (running lantern) (nanny level)
- Event trigger requirements of the function called by the event trigger
- Nacos 的安装与服务的注册
- Request preview display of binary data and Base64 format data
- Simple and beautiful method of PPT color matching
猜你喜欢
Usage Summary of scriptable object in unity
Google Maps case
SPSS analysis of employment problems of college graduates
Exponential weighted average and its deviation elimination
TCC of distributed solutions
【无标题】
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Arduino measures AC current
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
CJ mccullem autograph: to dear Portland
随机推荐
Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
查看网页最后修改时间方法以及原理简介
两数之和、三数之和(排序+双指针)
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
[untitled]
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
谷歌地图案例
派对的最大快乐值
Selenium+pytest automated test framework practice
【Note17】PECI(Platform Environment Control Interface)
VIM tail head intercept file import
Openresty ngx Lua regular expression
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
基于STM32的ADC采样序列频谱分析
Negative sampling
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Use of metadata in golang grpc
Common JVM tools and optimization strategies
视频标准二三事
Element positioning of Web Automation