A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:
1.85 2

题目大意:给一棵树,在树根出货物的价格为p,然后没往下走一层,价格增加r%,求所有叶子结点的最高价格,以及这个价格的叶子结点个数

分析:用二维数组v[i][j]存储,对于每一个结点i,它的孩子结点的下标push_back存储在v[i]中。用深度优先搜索dfs,保存当前结点的下标index以及当前结点所在层数,当当前结点的孩子结点个数为0的时候说明是叶子结点,更新maxdepth和maxnum的值,最后输出

注意:如果采用保存某个结点的父结点的下标的形式,然后一直遍历到根结点的深度/广度优先,会出现三个超时。因为从叶子结点往上遍历将会把所有路径都走一遍,很多都是重复走的路径,会超时,没有从根结点往下遍历的方式快~

记得r是百分比,要除以100之后再计算复利~

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int n, maxdepth = 0, maxnum = 0, temp, root;
vector<int> v[100010];
void dfs(int index, int depth) {if(v[index].size() == 0) {if(maxdepth == depth)maxnum++;if(maxdepth < depth) {maxdepth = depth;maxnum = 1;}return ;}for(int i = 0; i < v[index].size(); i++)dfs(v[index][i], depth + 1);
}
int main() {double p, r;scanf("%d %lf %lf", &n, &p, &r);for(int i = 0; i < n; i++) {scanf("%d", &temp);if(temp == -1)root = i;elsev[temp].push_back(i);}dfs(root, 0);printf("%.2f %d", p * pow(1 + r/100, maxdepth), maxnum);return 0;
}

1090. Highest Price in Supply Chain (25)-PAT甲级真题相关推荐

  1. 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  2. 1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. PAT甲级 -- 1090 Highest Price in Supply Chain (25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  4. 1090 Highest Price in Supply Chain (25)(25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  5. 1090. Highest Price in Supply Chain (25)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  6. 1090 Highest Price in Supply Chain (25)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  7. PAT:1090. Highest Price in Supply Chain (25) AC

    #include<stdio.h> #include<vector> using namespace std; const int MAX=100010; int DEPest ...

  8. 1090 Highest Price in Supply Chain (25 分)【难度: 一般 / 知识点: 树的遍历】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 就是找深度最深的叶节点的深度,和最深的结点个 ...

  9. 1090. Highest Price in Supply Chain (25) dfs

    #include <iostream> #include <vector> #include <cmath> using namespace std;int n, ...

最新文章

  1. python语言怎么输入-Python input()函数:获取用户输入的字符串
  2. delimiter mysql报错_MySql中的DELIMITER错误
  3. 既是客户又是供应商清帐配置
  4. P3076,jzoj3187-的士【贪心】
  5. ROS学习笔记(八): ROS通信架构
  6. Windows Print Spooler 远程代码执行漏洞(CVE-2021-1675)
  7. .o文件 linux生成,vmlinux.o 生成
  8. Linux下编写 makefile 详细教程
  9. 运行vue遇到的坑(续更
  10. java日期字符串排序_Java 8 字符串日期排序
  11. python调用vbs脚本_xShell终端调用VBS脚本 使用方法说明
  12. android开启wifi热点命令,Android便携式热点的开启状态检测和SSID的获取方法
  13. 搜狗收录方法之搜狗推送接口实现
  14. 笔记本电脑蓝牙搜索不到设备
  15. Unity Shader-GodRay,体积光(BillBoard,Volume Shadow,Raidal Blur,Ray-Marching)
  16. Android--Recovery模块之恢复出厂设置
  17. 那些短,却叫人很心疼的句子(转)
  18. Springboot----实现邮箱验证码登录(代码部分)
  19. HTTP内容的大小写问题
  20. Linux中安装IDA

热门文章

  1. Ext4中获取下拉框的值
  2. java反射经典实例 Java Reflection Cookbook
  3. 瞬间之美:Web 界面设计如何让用户心动
  4. RxSwift中Observable的各种创建方法
  5. 【JAQS】jaqs与DataCore 框架安装,学习笔记(一)
  6. HTTPS 加密算法原理详解
  7. ARM公司推出AI与机器学习用新型微处理器
  8. 折叠list ExpandableList+ BaseExpandableListAdapter 自定义 折叠列表
  9. FIN7 正在转向密码重置和软件供应链攻击
  10. 对 *.google.com/* 产品进行大规模的 CSRFing 研究,意外获得3万美元奖金