题目
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
10^5
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
S
i
S_i
Si is the index of the supplier for the 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%,求所有结点中的最高价格和这个价格的结点个数。
解答
因为价格最高的结点一定是叶子结点,通过BFS在遍历的时候得出每一颗结点的深度,然后遍历所有叶子结点即可得到最高价格和结点个数。DFS遍历中设置深度为参数,每个结点设置一个价格元素,在遍历中计算得到每个结点的价格,最后再按顺序遍历得到结点个数即可。
代码如下:
- 层序遍历(BFS)
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct node{
vector<int> child;
int layer;
double price;
}Node[maxn];
int n;
double p,r;
void layerOrder(int root){
queue<int> q;
q.push(root);
Node[root].layer=0;
while(!q.empty()){
int now = q.front();
q.pop();
if(Node[now].child.size()!=0){
for(int i=0;i<Node[now].child.size();i++) {
int temp = Node[now].child[i];
q.push(temp);
Node[temp].layer=Node[now].layer+1;
}
}
}
}
int main(){
int temp,root,count=0;
double ans=0,max=-1;
scanf("%d %lf %lf",&n,&p,&r);
r/=100;
for(int i=0;i<n;i++){
scanf("%d",&temp);
if(temp!=-1){
Node[temp].child.push_back(i);
}
else root = i;
}
layerOrder(root);
for(int i=0;i<n;i++){
if(Node[i].child.size()==0){
Node[i].price = p*pow(1+r,Node[i].layer);
//printf("%d %d %.2lf\n",i,Node[i].layer,pow(1+r,Node[i].layer));
if(max <= Node[i].price){
max = Node[i].price;
}
}
}
for(int i=0;i<n;i++){
if(Node[i].price == max) count++;
}
printf("%.2lf %d\n",max,count);
return 0;
}
- DFS
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct node{
vector<int> child;
int layer;
double price;
}Node[maxn];
int n,countA=0;
double p,r,maxA=-1;
void DFS(int root,int depth){
if(Node[root].child.size()==0){
Node[root].price = p*pow(1+r,depth);
if(Node[root].price >= maxA){
maxA = Node[root].price;
}
return;
}
for(int i=0;i<Node[root].child.size();i++){
DFS(Node[root].child[i],depth+1);
}
}
int main(){
int temp,root;
scanf("%d %lf %lf",&n,&p,&r);
r/=100;
for(int i=0;i<n;i++){
scanf("%d",&temp);
if(temp!=-1){
Node[temp].child.push_back(i);
}
else root = i;
}
DFS(root,0);
for(int i=0;i<n;i++){
if(Node[i].price == maxA) countA++;
}
printf("%.2lf %d",maxA,countA);
return 0;
}