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.
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
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 typedef struct NODE{ 7 vector<int>child; 8 }node; 9 node tree[100002], root; 10 int N; 11 double P, r; 12 int cnt = 0; 13 double maxSum = -1; 14 void dfs(int root, int depth){ //depth为包括root的层深度 15 double tempSum = 0; 16 if(tree[root].child.size() == 0){ 17 tempSum = P * pow(1.0 + r, depth * 1.0); 18 if(tempSum > maxSum){ 19 maxSum = tempSum; 20 cnt = 1; 21 }else if(tempSum == maxSum){ 22 cnt++; 23 } 24 return; 25 } 26 int len = tree[root].child.size(); 27 for(int i = 0; i < len; i++){ 28 dfs(tree[root].child[i], depth + 1); 29 } 30 } 31 32 int main(){ 33 int tempc, root; 34 scanf("%d%lf%lf", &N, &P, &r); 35 r = r / 100.0; 36 for(int i = 0; i < N; i++){ 37 scanf("%d", &tempc); 38 if(tempc == -1){ 39 root = i; 40 }else{ 41 tree[tempc].child.push_back(i); 42 } 43 } 44 dfs(root, 0); 45 printf("%.2f %d", maxSum, cnt); 46 cin >> N; 47 return 0; 48 }
总结:
1、仍然是求叶子节点的深度的问题。从根节点到叶子节点层层加价r%,求到叶子节点的最大价格,其实就是求最深的叶子节点深度。
2、题中说-1代表是根节点,最先还以为是说第i个数是-1,代表第i个节点的父亲是根节点。其实是说第i个节点是根节点。
3、题目中是以父亲节点的形式给出一棵树的(输入的第i个数字b,代表b的子节点是i)。