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 (<=10^5^), 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 S~i~ is the index of the supplier for the i-th member. S~root~ 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 10^10^.

Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:1.85 2

题目大意:给出一个数组a[i] = j; 表示i的供应商是j, 当a[i]=-1的时候,表示i是最顶端的供应商。求供应链的最长长度,以及处于最长长度供应链末端零售商的人数对给出的数据做以下处理:建立一个二维vector向量,chain[i]表示i为供应商,chain[i][j]表示j的供应商是i。chain[i].size()=0 则表示i是零售商            maxdepth记录供应链的最长长度,cnt记录处于最长供应链末端零售商的人数。这一题其实就是用数组建立树,并遍历树的过程。从顶端供应商root开始遍历,直到遍历到零售商为止(即chain[i].size()=0); 遍历过程中,如果发现当前的depth比maxdepth大,则更新maxd,并更新cnt=1;若遍历过程中发现当前depth=maxdepth则更新cnt的值;

注意点:  计算最高价格的时候,要用double类型, 用float类型,会有一些测试点不能通过,应该是溢出导致  在dfs()遍历树的时候,不能调用dfs(index, depth++), 应该用dfs(index, depth+1); 前者会改变同意层次循环中的depth值导致最终结果错误

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int cnt=0, maxdepth=0;
double ans=0.0;
vector<vector<int>> chain;
void dfs(int index, int depth){if(chain[index].size()==0){if(depth>maxdepth){maxdepth=depth; cnt=1;}else if(depth==maxdepth) cnt++;return;}for(int i=0; i<chain[index].size(); i++) dfs(chain[index][i], depth+1);
}
int main(){int n, i, root, temp;double p, r;cin>>n>>p>>r;chain.resize(n);for(i=0; i<n; i++){cin>>temp;if(temp==-1){root=i; continue;}chain[temp].push_back(i);}dfs(root, 0);printf("%.2f %d", p*pow((1.0+r/100.0), maxdepth), cnt);return 0;
}

转载于:https://www.cnblogs.com/mr-stn/p/9139221.html

1090 Highest Price in Supply Chain (25)相关推荐

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

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

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

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

  3. 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)-PAT甲级真题

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

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

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

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

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

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

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

  8. 1090 Highest Price in Supply Chain(甲级)

    1090 Highest Price in Supply Chain (25分) A supply chain is a network of retailers(零售商), distributors ...

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

    1090 Highest Price in Supply Chain (25 分) A supply chain is a network of retailers(零售商), distributor ...

最新文章

  1. grid - 使用相同的名称命名网格线和设置网格项目位置
  2. Blender 2.42
  3. [云炬创业管理笔记]第三章打造优秀创业团队讨论1
  4. mac环境下node.js和phonegap/cordova创建ios和android应用
  5. 找到符合条件的索引_高频面试题:MySQL联合索引的最左前缀匹配原则
  6. 一文带你读懂单目视觉SLAM数据关联优化
  7. Makefile入门(超详细一文读懂)
  8. 2020下半年软考中级网工答案
  9. 如何让测试RFC2544更便捷——RFC2544测试实操
  10. 索菲对讲机写频软件_万能对讲机写频软件
  11. 美国英语情景对话大全(zt)
  12. pycharm占用c盘
  13. 第一次见岳父岳母的攻略「转载」
  14. 500套优秀简历模板,送给您!
  15. 基于Java毕业设计在线交易系统源码+系统+mysql+lw文档+部署软件
  16. 学习写微信小程序(2)
  17. 浅谈设计模式在iOS开发实战项目中的应用
  18. Python学期总结:从无知到入门
  19. Spring源码解析十
  20. C/C++程序员简历模板(转载)

热门文章

  1. 开始使用gradle
  2. Python ValueError: IO operation on closed file
  3. 使用docker在CentOS7上搭建WordPress
  4. spring 设计模式
  5. Hadoop0.20.2版本在Ubuntu下安装和配置
  6. 基于zbus的MySQL透明代理(100行)
  7. 上班第一天(6)--一个程序员的成长史(15)
  8. Nodejs架构之json空处理
  9. 查看Linux上程序或进程用到的库
  10. mysql_load