1090 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

题目大意:题目大致意思与1079 Total Sales of Supply Chain 相同,无非就是所求结果相同,这道题让你输出可以卖出的最高价格和能卖出最高价格的销售商的人数。

大致思路:利用DFS从根节点往下搜索,记录每个叶子结点的编号方便后续统计价格相同的个数。

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;
struct node {
vector<int> child;
double sell;
}root[N];
double p, r;
int n;
vector<double> cost;
vector<int> leaf;
double ans = 0.0; void newNode (int x) {
root[x].sell = 0.0;
root[x].child.clear();
} void DFS(int x) {
if (root[x].child.size() == 0) {
ans = max(ans, root[x].sell);
leaf.push_back(x);
return;
}
for (int i = 0; i < root[x].child.size(); i++) {
int tmp = root[x].child[i];
root[tmp].sell = (1 + 0.01 * r) * root[x].sell;
DFS(tmp);
}
} int main() {
scanf("%d%lf%lf", &n, &p, &r);
int root_index;
for (int i = 0; i < n; i++) newNode(i);
for (int i = 0; i < n; i++) {
int s;
scanf("%d",&s);
if (s == -1) {
root_index = i;
continue;
}
root[s].child.push_back(i);
}
root[root_index].sell = p;
DFS(root_index);
int cnt = 0;
for (auto l : leaf) {
if (root[l].sell == ans) cnt++;
}
printf("%.2lf %d\n", ans, cnt);
return 0;
}

最新文章

  1. RSA的傻瓜原理
  2. 【原】iOS动态性(二):运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)
  3. js-JavaScript高级程序设计学习笔记1
  4. OC-copy
  5. Android runProguard配置 导致module lib 中的包编译时无法识别
  6. MVP Community Camp 社区大课堂
  7. Mac OS 上设置 JAVA_HOME
  8. onsubmit提交前先验证(验证不通过阻止form提交)
  9. Working with Numbers in PL/SQL(在PL/SQL中使用数字)
  10. svn问题(队列)
  11. iOS 沙盒
  12. Scala学习笔记(二)
  13. 你知道如何为iOS工程改名吗?
  14. git add 添加多个文件
  15. HDU 1049(蠕虫爬井 **)
  16. SpringBoot 注解
  17. Java设计模拟菜单
  18. IE浏览器调试工具不能使用
  19. 测试sql server服务是否配置正确
  20. ubuntu18.04配置nvidia docker和远程连接ssh+远程桌面连接(二)

热门文章

  1. java架构《并发线程基础二》
  2. docker(11)Dockerfile 中的COPY与ADD 命令
  3. xxl-job之实现流程任务编排思路
  4. HBase原理 – 分布式系统中snapshot是怎么玩的?(转载)
  5. 小希的迷宫B - B
  6. hdu1828 Picture(线段树+扫描线+矩形周长)
  7. Pyqt5使用
  8. String的20个方法
  9. Leetcode(83)-删除排序链表中的重复元素
  10. Leetcode(3)-无重复字符的最长子串