题目

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;
}

PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]的更多相关文章

  1. [建树(非二叉树)] 1090. Highest Price in Supply Chain (25)

    1090. Highest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors ...

  2. 1090. Highest Price in Supply Chain (25) -计层的BFS改进

    题目如下: A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyon ...

  3. PAT 甲级 1090 Highest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...

  4. 1090. Highest Price in Supply Chain (25)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  5. 1090 Highest Price in Supply Chain (25)(25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  6. PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...

  7. PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)

    简单dfs. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 【PAT甲级】1090 Highest Price in Supply Chain (25 分)

    题意: 输入一个正整数N(<=1e5),和两个小数r和f,表示树的结点总数和商品的原价以及每向下一层价格升高的幅度.下一行输入N个结点的父结点,-1表示为根节点.输出最深的叶子结点处购买商品的价 ...

  9. 1090. Highest Price in Supply Chain (25)-dfs求层数

    给出一棵树,在树根出货物的价格为p,然后每往下一层,价格增加r%,求所有叶子节点中的最高价格,以及该层叶子结点个数. #include <iostream> #include <cs ...

随机推荐

  1. Django(十二)视图--利用jquery从后台发送ajax请求并处理、ajax登录案例

    一.Ajax基本概念 [参考]:https://www.runoob.com/jquery/jquery-ajax-intro.html 异步的javascript.在不全部加载某一个页面部的情况下, ...

  2. LeetCode1029 两地调度(贪心+java自定义排序回顾)

    题目: 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵 ...

  3. MongoDB 初始化数据同步

    MongoDB初始化数据同步: 副本集中的成员启动之后,就会检查自身的状态,确定是否可以从某个成员那里进行同步.如果不行的话,尝试从其他成员那里进行完整的数据复制. 这个过程就是初始化同步(initi ...

  4. xv6 锁

    在xv6 中锁对象是 spinlock,spinlock中的locked为1的时候表示被占用,为0的时候锁空闲. struct spinlock { uint locked; // Is the lo ...

  5. 面向对象设计思想和MVC设计模式

    虽然之前学习Java时有接触过面向对象的设计思想,但因当时Java没学好.所以导致这两天讲php的面向对象设计时,感到没有头绪,这也反应了我练习少和逻辑能力的不足.而MVC设计思想 面向对象就是要将系 ...

  6. ES6 之 对象属性的可枚举性和遍历

    1.Object.getOwnPropertyDescriptor() 解释:获取对对象属性的描述对象. let obj = { foo: 123 }; console.log(Object.getO ...

  7. MFC消息映射及消息处理函数原型

    MFC把消息主要分为三大类: 1. 标准Windows消息(WM_XXX) 2. 命令消息(WM_COMMAND):凡由UI对象产生的消息都是这种命令消息,可能来自菜单或加速键或工具栏按钮. 3. 控 ...

  8. 【LeetCode】验证二叉搜索树

    [问题]给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征:节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是二叉搜 ...

  9. cf 766#

    天呢,太垃圾了我.. AB懵逼了半天题意,C最后搞了个DP还不对...DP太垃圾了,, #include<bits/stdc++.h> #define INF 0x7fffffff #de ...

  10. bzoj 4236JOIOJI

    一开始忘掉特殊情况也是蛋疼2333(有一直到头的.mp[0][0]是要特判的) 做法也就是找mp[i][j]相同的东西.(貌似可以写成线性方程组(z=x+A,z=y+B)过这个的就是相等(可以先从2维 ...