[COCI2017-2018 Contest5] Birokracija
题目描述
Mirko has become CEO of a huge corporation. This corporation consists of N people, labeled from 1 to N , and Mirko is labeled 1. The corporation is structured so that each employee (except Mirko) has a boss, and we say that employee is an assistant to that boss. Each boss can have multiple assistants, but still reports to their boss. This holds for everyone except Mirko, who is at the top of the pyramid and doesn’t have a boss, but has his assistants.
When Mirko gets a task from the investors, he then delegates that task to his assistant with the minimal number. This assistant then delegates the task to their assistant with the minimal number, and this process repeats until the task is given to an unlucky person without an assistant, who then must do the task.
This is when the real problems begin. The person that did the task gets paid 1 coin, the boss of that person gets 2 coins, the boss of that person gets 3 coins, and so on, all the way to Mirko, who gets as many coins as there are people in this sequence. After that, the employee that really did the job realizes how unfair the system is and quits their job out of principle.
When it comes to doing the next task in the corporation, there is a person less, so maybe the paychecks are less, but the work must continue. Tasks are piling up, so the whole procedure (assigning a new task, doing it, dividing paychecks and the person doing the task quitting) repeats until Mirko is left alone in the corporation and does his first (also his last) task.
Of course, Mirko will have amassed quite a fortune until then, but he also wants to know how much money each of the employees earned.
输入输出格式
输入格式:
The first line of input contains the positive integer N (2 ≤ N ≤ 2· 10^5105 ), the number of employees (including Mirko).
The following line contains N- 1 positive integers a_2a2 , a_3a3 , a_4a4 , …, a_nan (1 ≤ a_iai < i ), where a_iai denotes the boss of employee i .
输出格式:
You must output a single line consisting of N numbers, the i^{th}ith number corresponding to the amount of money earned by the i^{th}ith employee.
输入输出样例
- 3
- 1 1
- 5 1 1
- 5
- 1 2 2 4
- 13 8 1 3 1
说明
In test cases worth 50% of total points, it will hold 2 ≤ N ≤ 5000.
Clarification of the second test case:
Mirko assigns the first task to employee 2, who then assigns it to employee 3, who then does it. For this, employee 3 gets 1 coin, employee 2 gets 2 coins, and employee 1 (Mirko) gets 3 coins. Employee 3 then quits.
Mirko assigns the second task to employee 2, who then assigns it to employee 4 (because employee 3 quit), who then assigns it to employee 5, who then does it. For this, employee 5 gets 1 coin, employee 4 gets 2 coins, employee 2 gets 3 coins, and employee 1 gets 4 coins. Employee 5 then quits.
The procedure is repeated for a total of 5 tasks.
In total, Mirko gets 13 coins, employee 2 gets 8, employee 4 gets 3, and employee 3 and 5 each get 1 coin.
考虑每个点对其父亲的贡献肯定是自身的答案再加上自身的size,所以一遍dfs就好了。
- #include<bits/stdc++.h>
- #define ll long long
- using namespace std;
- #define pb push_back
- const int maxn=200005;
- vector<int> g[maxn];
- int siz[maxn],n,pre;
- ll ans[maxn];
- void dfs(int x){
- siz[x]=1;
- for(int i=g[x].size()-1,to;i>=0;i--){
- to=g[x][i],dfs(to);
- ans[x]+=ans[to],siz[x]+=siz[to];
- }
- ans[x]+=(ll)siz[x];
- }
- int main(){
- scanf("%d",&n);
- for(int i=2;i<=n;i++) scanf("%d",&pre),g[pre].pb(i);
- dfs(1);
- for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
- puts("");
- return 0;
- }
[COCI2017-2018 Contest5] Birokracija的更多相关文章
- COCI2017/2018 CONTEST #7
Prosjek 显然,越大的数应该越后参与平均数的计算,这样受较小数的影响就小一些 那我们就排个序,贪心的从最小的数开始往大的计算平均数即可 时间复杂度\(O(nlogn)\) Timovi 把分组分 ...
- 2018. The Debut Album
http://acm.timus.ru/problem.aspx?space=1&num=2018 真心爱过,怎么能彻底忘掉 题目大意: 长度为n的串,由1和2组成,连续的1不能超过a个,连续 ...
- Math.abs(~2018),掌握规律即可!
Math.abs(~2018) 某前端群的入门问题长姿势了,一个简单的入门问题却引发了我的思考,深深的体会到自己在学习前端技术的同时忽略遗忘了一些计算机的基础知识. 对于 JS Math对象没什么可说 ...
- 肖秀荣8套卷2018pdf下载|2018肖秀荣冲刺8套卷pdf下载电子版
肖秀荣8套卷2018pdf下载|2018肖秀荣冲刺8套卷pdf下载电子版 下载链接: https://u253469.ctfile.com/fs/253469-229815828
- 2018年的UX设计师薪酬预测,你能拿多少?
以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 一个经验丰富的设计师完全可以根据地区和专业来可以预期薪酬之间的差距,其中悬殊最高可达80K. 本 ...
- Hello 2018, Bye 2017
2017年过去了,过去一年经历了太多,改变了好多好多,可以说人生进入了另一个阶段,有可能是成熟吧. 回顾2017 去年换了新工作,离开了将近工作了8年的公司,不带走一丝云彩,为其任劳任怨,最后没有任何 ...
- New Life With 2018
2017年转眼过去了.对自己来说.这一大年是迷茫和认知的一年.我的第一篇博客就这样记录下自己的历程吧 一:选择 从进入这一行到现在已经一年多了,2016年11月份就像所有的应届毕业生一样,都贼反感毕业 ...
- 2017 年终总结 & 2018 年度计划
不立几个 Flag,都不知道怎么作死 2017 年度计划完成情况: 1.健身时间不少于350天: 未完成 中断了22天,实际运动 343天 2.至少每个月看一本书: 及格 <切尔诺贝利的 ...
- [总结]-2018 w1
不想总结 2017,过去的就过去吧,不过自己在 2017 年还是收获了很多,最重要的就是赚钱.赚钱还是需要两把刷子,所以,2018 的小目标就是学习数据分析和机器学习.希望自己在这两个领域能搞点事情. ...
随机推荐
- about 2018
2018想要完成的10件事情 1 活出更纯粹的自己. 未完成2 自考本科一定要过. ...
- 线段树(单点更新,区间查询) HDU 1754 I Hate It
题目链接 线段树的模板 #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- 【Python】重定向 Stream 到文件
Python 系统模块 sys 中有三个变量 stdin . stdout 与 stderr ,分别对应标准输入流.输出流与错误流.stdin 默认指向键盘, stdout 与 stderr 默认指向 ...
- React03 移动端跨平台开发
目录 React-day03 RN移动端开发 了解React-Native 了解React-Native工作流程 创建第一个React-Native项目 * 了解React-Native项目及结构 开 ...
- iptables的配置文件/etc/sysconfig/iptables不存在 linux防火墙开关命令
某linux服务器,使用 cat /etc/sysconfig/iptables命令时, 找不到文件. 1. service iptables status 使用该命令检查状态 如果之前找不到配置文件 ...
- Android记事本开发03
昨天: 生成签名文件及导出apk 遇到的问题: 无. 今天: activity和intent基础
- TJOI2018游记
D1T1 - 数学计算 直接用线段树/平衡树维护所有数的积即可.我思想僵化写了一个数学方法...应该是能做\(\bmod\)所有数的乘除法. 时间复杂度\(O(nlogn)\). D1T2 - 智力竞 ...
- PL/SQL 查询结果集直接修改数据
使用t.rowid,查询可以直接在查询结果中修改提交 SELECT t.rowid,t.* from UC_ROLE t where ROLE_NAME like '% %'
- Windows彻底删除不用的dc
如果DC迁移,或者多台DC中的某台DC损坏,要退出历史舞台,一定要彻底的卸载,否则,系统会默认存在,一直同步,会出很多问题.怎么做才能彻底的从域中卸载呢?下面介绍一个个人认为很好用的方法:1.在存活的 ...
- Velocity模版使用
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity --> <dependency> &l ...