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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef struct NODE{
vector<int>child;
}node;
node tree[], root;
int N;
double P, r;
int cnt = ;
double maxSum = -;
void dfs(int root, int depth){ //depth为包括root的层深度
double tempSum = ;
if(tree[root].child.size() == ){
tempSum = P * pow(1.0 + r, depth * 1.0);
if(tempSum > maxSum){
maxSum = tempSum;
cnt = ;
}else if(tempSum == maxSum){
cnt++;
}
return;
}
int len = tree[root].child.size();
for(int i = ; i < len; i++){
dfs(tree[root].child[i], depth + );
}
} int main(){
int tempc, root;
scanf("%d%lf%lf", &N, &P, &r);
r = r / 100.0;
for(int i = ; i < N; i++){
scanf("%d", &tempc);
if(tempc == -){
root = i;
}else{
tree[tempc].child.push_back(i);
}
}
dfs(root, );
printf("%.2f %d", maxSum, cnt);
cin >> N;
return ;
}

总结:

1、仍然是求叶子节点的深度的问题。从根节点到叶子节点层层加价r%,求到叶子节点的最大价格,其实就是求最深的叶子节点深度。

2、题中说-1代表是根节点,最先还以为是说第i个数是-1,代表第i个节点的父亲是根节点。其实是说第i个节点是根节点。

3、题目中是以父亲节点的形式给出一棵树的(输入的第i个数字b,代表b的子节点是i)。

上一篇:Code First研究学习1_Reverse Enginner Code First


下一篇:Python 中 mySQL 中的语句