目录

​1,题目描述​

​ 题目大意​

​2,思路​

​3,AC代码​

​4,解题过程​


1,题目描述

PAT_甲级_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】_C++

Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

 

Sample Output:

1.85 2

 题目大意

求一个供应链中,货物可以卖出的最高价,以及卖出最高价的零售商数目

  • each number S​i​​ is the index of the supplier for the i-th member.:编号为0到N-1,Si即供应商,下标即商家的编号,比如3th=4,供应商4为商家3提供货物;

 

2,思路

和这一题很像​​@&再见萤火虫&【PAT_甲级_1079 Total Sales of Supply Chain (25point(s)) (C++)【DFS/scanf接受double】】​

简单的DFS搜索,记录遍历的层数即可:

PAT_甲级_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】_C++_02

PAT_甲级_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】_DFS_03

3,AC代码

#include<bits/stdc++.h>
using namespace std;
double P, r;
int N, maxDep = 0, num = 0;
vector<int> data[100003];
void dfs(int r, int dep){
if(data[r].size() == 0){
if(dep > maxDep){
maxDep = dep;
num = 1;
}else if(dep == maxDep)
num++;
}
for(int i = 0; i < data[r].size(); i++)
dfs(data[r][i], dep + 1);
}
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d %lf %lf", &N, &P, &r);
int id, root;
for(int i = 0; i < N; i++){
scanf("%d", &id);
if(id == -1)
root = i;
else
data[id].push_back(i);
}
dfs(root, 0);
printf("%.2f %d", P * pow(1 + r / 100, maxDep), num);
return 0;
}

 

4,解题过程

一发入魂

PAT_甲级_1090 Highest Price in Supply Chain (25point(s)) (C++)【DFS】_PAT_04