题目
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 ( ≤ 1 0 5 ) N(\le10^5) N(≤105), the total number of the members in the supply chain (and hence they are numbered from 0 to N − 1 N-1 N−1); P P 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 N N numbers, each number S i S_i Si is the index of the supplier for the i i i-th member. S r o o t S_{root} 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 1 0 10 10^{10} 1010.
Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2
题目大意
商品供应链问题,商品每流通一环,价格会上涨 r r r 百分比,商品经中间商到零售商销售,流通环节多的商品价格就高一些,现在要求出流通的最高价格以及以最高价格销售的零售商数量。
思路
先画出供应链图,明显是一道树的问题,即树的层次遍历,没想下遍历一层,价格上涨相应百分比,同时记录每一层的结点数,最底层结点数即为答案。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n, root, cnt, level = 0;
double p, r, ansp;
vector<vector<int> > mp(100000);
scanf("%d %lf %lf", &n, &p, &r);
for(int i=0, t; i<n; i++){
scanf("%d", &t);
if(t == -1)
root = i;
else
mp[t].push_back(i);
}
queue<int> que;
que.push(root);
while(!que.empty()){
int t = que.size();
for(int i=0; i<t; i++){
int temp = que.front();
for(int j=0; j<mp[temp].size(); j++)
que.push(mp[temp][j]);
que.pop();
}
cnt = t;
level++;
}
double hprice = p;
for(int i=1; i<level; i++)
hprice *= (1.0 + r*0.01);
printf("%.2lf %d\n", hprice, cnt);
return 0;
}