PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]
题目
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
- dfs遍历树,参数p记录当前节点所在层的价格,max_p记录最大层,max_p_num记录最大层叶子结点数
思路 02
- 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;
}
PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]的更多相关文章
- [建树(非二叉树)] 1090. Highest Price in Supply Chain (25)
1090. Highest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors ...
- 1090. Highest Price in Supply Chain (25) -计层的BFS改进
题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...
- PAT 甲级 1090 Highest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...
- 1090. Highest Price in Supply Chain (25)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...
- 1090 Highest Price in Supply Chain (25)(25 分)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)
简单dfs. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- 【PAT甲级】1090 Highest Price in Supply Chain (25 分)
题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价 ...
- 1090. Highest Price in Supply Chain (25)-dfs求层数
给出一棵树,在树根出货物的价格为p,然后每往下一层,价格增加r%,求所有叶子节点中的最高价格,以及该层叶子结点个数. #include <iostream> #include <cs ...
随机推荐
- Django(八)模型:Mysql8.0日志文件配置开启/关闭、查询
Mysql日志文件开启.配置.查看 mysql.log是mysql的日志文件,里面记录的对MySQL数据库的操作记录.默认情况下mysql的日志文件没有产生,需要修改mysql的配置文件,步骤如下: ...
- linux扩容空间,再扩容文件系统
Linux磁盘空间进行扩容 参考博客 http://blog.csdn.net/dingchenxixi/article/details/50986472 http://blog.sina.com.c ...
- HDU - 6198 number number number(规律+矩阵快速幂)
题意:已知F0 = 0,F1 = 1,Fn = Fn - 1 + Fn - 2(n >= 2), 且若n=Fa1+Fa2+...+Fak where 0≤a1≤a2≤⋯≤a,n为正数,则n为mj ...
- UVA - 10891 Game of Sum (区间dp)
题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...
- VMware CentOS网络配置
- mybatis关于级联查询结果集嵌套映射对象非列表的处理问题
工作中遇到这么一个问题,嵌套查询,返回json的时候,作为属性,deviceFields是一个device中的一个对象属性,在json返回的时候想要得到的应该是deviceFields:{ 具体属性} ...
- cf 763A. Timofey and a tree
呵呵呵,直接判断是不是一个点连起来所有的特殊边(连接2不同颜色的点的边) (一开始还想各种各样奇怪的dfs...垃圾) #include<bits/stdc++.h> #define LL ...
- 15.swoole学习笔记--异步写入文件
<?php //异步写入文件 $content="hello world"; swoole_async_writefile('2.txt',$content,function ...
- springMvc接收json和返回json对象
导入三个包 页面: function sendJson(){ //请求json响应json $.ajax({ type:"post", url: "${pageConte ...
- linux项目,项目报错,排查
今天在遇到以前部署的项目突然没有数据了,然后就去服务器看了一下,打印日志发现报错了,现在我是一脸懵逼,因为不知道怎么排查 然后同事告诉说先看报错的原因,然后再去找认识的类,我打码的都是一些认识的 然后 ...