题目

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(<=10^5), 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

题目分析

已知所有非叶子节点的所有子节点,求最大层数和最大层叶子结点数

解题思路

思路 01

  1. dfs遍历树,参数p记录当前节点所在层的价格,max_p记录最大层,max_p_num记录最大层叶子结点数

思路 02

  1. bfs遍历树,double ps[n]数组记录当前节点所在层的价格,max_p记录最大层,max_p_num记录最大层叶子结点数

Code

Code 01(dfs)

#include <iostream>
#include <vector>
using namespace std;
const int maxn=100000;
double p,r,max_p;
vector<int> nds[maxn];
int max_p_num;
void dfs(int index, double p) {
if(nds[index].size()==0) {
if(max_p<p) {
max_p=p;
max_p_num=1;
} else if(max_p==p) {
max_p_num++;
}
return;
}
for(int i=0; i<nds[index].size(); i++) {
dfs(nds[index][i],p*(1+r*0.01));
}
}
int main(int argc, char * argv[]) {
int n,rid,root;
scanf("%d %lf %lf", &n,&p,&r);
for(int i=0; i<n; i++) {
scanf("%d",&rid);
if(rid==-1)root=i;
else nds[rid].push_back(i);
}
dfs(root,p);
printf("%.2f %d",max_p,max_p_num);
return 0;
}

Code 02(bfs)

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int maxn=100000;
double p,r,max_p,ps[maxn];
vector<int> nds[maxn];
int max_p_num,root;
void bfs() {
queue<int> q;
q.push(root);
while(!q.empty()) {
int now = q.front();
q.pop();
if(nds[now].size()==0) {
if(max_p==ps[now]) {
max_p_num++;
} else if(max_p<ps[now]) {
max_p=ps[now];
max_p_num=1;
}
} else {
for(int i=0; i<nds[now].size(); i++) {
ps[nds[now][i]]=ps[now]*(1+r*0.01);
q.push(nds[now][i]);
}
}
}
}
int main(int argc,char * argv[]) {
int n,rid;
scanf("%d %lf %lf", &n,&p,&r);
for(int i=0; i<n; i++) {
scanf("%d",&rid);
if(rid==-1)root=i;
else nds[rid].push_back(i);
}
ps[root]=p;
bfs();
printf("%.2f %d",max_p,max_p_num);
return 0;
}

最新文章

  1. 每周一书《Oracle 12 c PL(SQL)程序设计终极指南》
  2. 因GIT默认忽略.dll文件导致的Visual Studio项目通过Bamboo编译失败
  3. 关于如何使用Altium Designer 10以上版本官方库
  4. JSP+servlet简单登录实例
  5. java中图片文件的传输及显示(Socket以及ServerSocket演示)
  6. Core Data数据操作
  7. css修改,类似elememt.style样式修改
  8. elipse 调试 反射 invoke 子类
  9. Nginx开启Gzip压缩大幅提高页面加载速度(转)
  10. 转:40多个关于人脸检测/识别的API、库和软件
  11. 【iOS7一些总结】9、与列表显示(在):列表显示UITableView
  12. zoj 1152 A Mathematical Curiosity
  13. Java获取异常堆栈信息
  14. 腾讯AlloyTeam正式发布pasition - 制作酷炫Path过渡动画
  15. 【BZOJ4566】找相同字符(后缀自动机)
  16. Collection集合详解
  17. mac电脑php+mysql+nginx+phpmyadmin环境搭建
  18. PHPstorm配置SVN的问题
  19. 细看Thread的 start() 和 run()方法
  20. java用Thread方式创建多线程

热门文章

  1. delphi中json转dataset
  2. Java中用单例模式有什么好处?
  3. 洛谷P1002——过河卒
  4. 004、mysql使用SHOW语句找出服务器上当前存在什么数据库
  5. MacType
  6. oracle获取排序第一的数据
  7. mysql 统计值为NULL不为0的问题
  8. super方法
  9. spring学习第7天(PCD以及切点表达式)
  10. 【剑指Offer】面试题09. 用两个栈实现队列