[PAT A1090]Highest Price in Supply Chain

[PAT A1090]Highest Price in Supply Chain

题目描述

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.

输入格式

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.

输出格式

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}.

输入样例

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

输出样例

1.85 2

解析

1.题目大意:与[PAT A1079]Total Sales of Supply Chain背景如出一辙,这里就不再赘述,详细可见[PAT A1079]Total Sales of Supply Chain

2.这里题目只不过改变了输入格式和输出要求,输入N个数,第i个数表示的是编号为i-1的结点的父亲结点的编号,需要求的是最大的销售单价和最大销售单价的商家个数->转化为求叶节点的个数

3.具体代码如下:

#include
#include
#include
using namespace std;
const int maxn = 100010;  //最大的商家个数
struct node
{
	int level = 0; //用于存储每个结点所在的层数
	vector childs;  //子结点的编号数组
};
int n;
double p, r;
node tree[maxn];
void traverse(int i, int level);
int main()
{
	int root = -1;
	scanf("%d %lf %lf", &n, &p, &r);
	for (int i = 0; i < n; i++) {
		int temp;
		scanf("%d", &temp);
		if (temp >= 0) {
			tree[temp].childs.push_back(i);
		}
		else root = i;  //如果输入为-1,代表改结点为根结点
	}
	int maxl = -1, cnt = 0;  //存储最深的层数和对应最深的结点的个数
	traverse(root, 0);
	for (int i = 0; i < n; i++) if (tree[i].level > maxl) maxl = tree[i].level;  //求最深层数
	for (int i = 0; i < n; i++) if (tree[i].level == maxl) cnt++;  //记录个数
	printf("%.2lf %d", p*pow(r / 100 + 1, maxl), cnt);
	return 0;
}
void traverse(int i,int level) { //遍历树
	if (i == -1) return;
	tree[i].level = level;
	for (int j = 0; j < tree[i].childs.size(); j++) traverse(tree[i].childs[j], level + 1);
}

 

你可能感兴趣的:(C++)