A1090 Highest Price in Supply Chain (25 point(s))

DFS求到叶结点路径最长及个数

1. 原文

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 S**iis 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 10^10.

Sample Input:

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

Sample output:

1
1.85 2

2. 解析

i是被指向的, v[a].push_back(i); i为-1的是根

dfs遍历到叶结点v[root].size()==0 判断当前记录的深度deep,大于原来记录的maxdeep,最深的路径为cnt=1,等于maxdeep,cnt++;

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
vector<int> v[100010];
int maxdeep=-1;
int cnt=1;
void dfs(int root,int deep){
if (v[root].size()==0)
{
if (deep>maxdeep)
{
maxdeep=deep;
cnt=1;
}else if (deep==maxdeep){
cnt++;
}
return;
}
for (int i = 0; i < v[root].size(); ++i)
{
dfs(v[root][i],deep+1);
}
}
int main()
{
int n; int root=0;
double p,r;
scanf("%d%lf%lf",&n,&p,&r);
int a;
for (int i = 0; i < n; ++i)
{
scanf("%d",&a);
if (a!=-1)
{
v[a].push_back(i);
}else{
root=i;
}
}
dfs(root,0);
printf("%.2f %d\n", p*pow((1+r/100),maxdeep),cnt);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1090/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!