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 ...
随机推荐
- C++ 语言程序设计(清华大学)1
1.回文数字判断方法(逆过来数值相等):y=y*10+x%10; x /= 10; return(x==y) 2.int rand(void)函数,所需头文件<cstdlib> ,功能是求 ...
- 觉醒 闭包/this
两条非常重要的知识点 首先上题 var a = '322' var preview = function() { var a = '123' console.log(a, this, this.a) ...
- [JZOJ]3413.KC的瓷器
Description KC来到了一个盛产瓷器的国度.他来到了一位商人的店铺.在这个店铺中,KC看到了一个有n(1<=n<=100)排的柜子,每排都有一些瓷器,每排不超过100个.那些精美 ...
- 十八、SAP中使用IF/ELSE判断语句,以及sy-subrc的用法
一.sy_subrc为上一条语句的执行结果,如果为0,则表示执行成功. 需要注意的是,IF ELSE语句,每一个关键字都需要带句号 二.执行效果如下 相关代码如下 *&------------ ...
- 163-PHP 文本替换函数str_replace(四)
<?php $str='Hello world!'; //定义源字符串 $search=array('o','l','w'); //定义将被替换的字符数组 $replace=array('1', ...
- HZNU-ACM寒假集训Day12小结 数论入门 题解
算不出的等式 BJOI2012 看到这题 真没什么办法 无奈看题解 1.注意到p/q 联想到斜率 2.注意到 [ ] 联想到整点 注意到k在变化,构造一次函数 f(x)=p/q*x ,g(x)=q/p ...
- HZNU-ACM寒假集训Day11小结 贪心
1.刘汝佳紫书区间问题三大情况 1.选择不相交区间 贪心策略:一定要选择第一个区间 2.区间选点问题 贪心策略:取最后一个点 3.区间覆盖问题: n个闭区间,选择尽量少的区间覆盖一条指定线段[s,t] ...
- 报错SQL盲注之BIGINT 溢出
首先感谢原创博主,在此致敬.本文转自:http://www.cnblogs.com/lcamry/articles/5509112.html MySQL版本在 5.5.5 及其以上 0x01 概述 我 ...
- java课程课后作业190502之单词统计
自己想的方法一直都不是很好,但是又一直忘了改自己的算法,只能硬着头皮接着用自己以前的老方法了 第0步:输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后 ...
- Python爬虫实战(一) 使用urllib库爬取拉勾网数据
本笔记写于2020年2月4日.Python版本为3.7.4,编辑器是VS code 主要参考资料有: B站视频av44518113 Python官方文档 PS:如果笔记中有任何错误,欢迎在评论中指出, ...