SGU - 195 New Year Bonus Grant ( 贪心

本文介绍了一个关于微软公司新年奖金分配的问题。为了最大化员工收益并公平分配,文章提出了一个树形结构的染色问题解决方案,通过从叶子节点开始的贪心算法来确定最优的奖金分配方案。

New Year Bonus Grant

题目描述

All programmers of Mocrosoft software company are organized in a strict subordination hierarchy. Every programmer has exactly one chief, except Bill Hates who is also the head of the company and has no chief.

Due to the celebration of the new 2003 year, chief accountant of Mocrosoft decided to pay a New Year Bonus Grant of 1000 dollars to some programmers. However being extremely concerned of the company wealth she would like to designate the least possible amount of money for these grants. On the other hand she didn’t want to be accused of being too greedy or of giving preferences to some programmers. To do this, she developed the following scheme of grants appointment:

Each programmer may either assign a grant to one of his subordinates or have a grant assigned to him by his chief or none of the above.
No programmer can simultaneously receive a grant and assign a grant to one of his subordinates.
No programmer can assign a grant to more than one of his subordinates

The scheme seemed to be designed perfectly — nobody would like to assign a grant to anybody since in this case he himself would not receive money. But programmers somehow discovered the plan of chief accountant and decided to make a trick to get the most money possible and share them fairly afterwards. The idea was to make such grant assignments that the total amount of grant money received is maximum possible.

You were selected to write the program which will find the optimal grants appointment.

输入

The first line of the input file contains integer N — the number of programmers in Mocrosoft company (2 ≤ N ≤ 500 000). Each programmer is assigned his unique identifier — integer number ranging from 1 to N. Bill Hates has number 1 and each programmer has the number greater then the number of his chief. The second line of the input file contains N-1 integers, i-th of which being the number of the chief of the worker whose number is (i + 1).

输出

On the first line of the output file print the maximum possible amount of money workers can get. On the second line output the numbers of programmers that will receive grant in ascending order.

样例

Input
4 
1 1 2 

Output
2000 
3 4 

题意

挺简单的一道题 就是体面太长了,,,
问题抽象为树的染色问题,我们可以知道 染色条件是
节点如果染色, 那么所有和他的父节点,兄弟节点,子节点都不能染色
节点不染色,那么就可以将且仅可以将自己的一个子节点染色
求的是染色节点尽可能个的多 那么 我们可以从叶子节点 不端向上更新,这样做的优点就是可以不考虑自己兄弟节点的情况,这个是一个贪心的过程

AC代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))
#define ls st<<1
#define rs st<<1|1

const int MAXN = 1e6+10;
int fa[MAXN];
bool vis[MAXN];
int sum[MAXN];
int main() {
    int n;
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i = 2; i <= n; i++) {
        cin >> fa[i];
    }
    int k = 0;
    for(int i = n; i >= 2; i--) {
        if(!vis[i] && !vis[fa[i]]) {
            vis[i] = true;
            vis[fa[i]] = true;
            sum[k++] = i;
        }
    }
    cout << k*1000 << endl;
    for(int i = k-1; i >= 1; i--) {
        cout << sum[i] << " ";
    }
    cout << sum[0] << endl;

return 0;
}
基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB代码 动态避障路径规划:基于部落竞争与成员合作算法(CTCM)融合动态窗口法DWA的无人机三维动态避障方法研究,MATLAB 融合DWA的青蒿素优化算法(AOA)求解无人机三维动态避障路径规划,MATLAB代码 基于动态环境下多智能体自主避障路径优化的DWA算法研究,MATLAB代码 融合DWA的青蒿素优化算法AOA求解无人机三维动态避障路径规划,MATLAB代码 基于DWA的多智能体动态避障路径规划算法研究,MATLAB代码 融合动态窗口法DWA的粒子群算法PSO求解无人机三维动态避障路径规划研究,MATLAB代码 基于粒子群算法PSO融合动态窗口法DWA的无人机三维动态避障路径规划研究,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于ACOSRAR-DWA无人机三维动态避障路径规划,MATLAB代码 基于DWA的动态环境下无人机自主避障路径优化,MATLAB代码 基于DWA的动态环境下机器人自主避障路径规划,MATLAB代码 基于城市场景下RRT、ACO、A*算法的无人机三维路径规划方法研究,MATLAB代码 基于城市场景下无人机三维路径规划的导航变量的多目标粒子群优化算法(NMOPSO),MATLAB代码 导航变量的多目标粒子群优化算法(NMOPSO)求解复杂城市场景下无人机三维路径规划,MATLAB代码 原创:5种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 原创:4种最新多目标优化算法求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),MATLAB代码 高维超多目标优化:基于导航变量的多目标粒子群优化算法(NMOPSO)的无人机三维
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值