PAT

mac2025-07-23  6

题意:此题大意是经销商和零售商分别到经销商和供应商取货的问题,题目中说了不存在回路,即可以看成是一个二叉树,没经过过一个节点价格就按照给定的倍率提高价格,要求零售商售价最高的价格,而且此最高价格相同的有多少个。

思路:求最大的零售价格,转化位求最长的路径,路径求出来了就可以算出售价为多少了,求有多少个最高价格相同的即求最长的路径有多少条

代码:

#include<iostream> #include<math.h> #include<vector> using namespace std; vector<int> p[100010]; int level = -1,cnt = 0; void dfs(int root,int depth) {//深度遍历 if (p[root].size() == 0) { if (level < depth) { level = depth; cnt = 1; } else if (level == depth) { cnt++; } return; } for (int i = 0; i < p[root].size(); i++) { dfs(p[root][i],depth+1); } } int main() { int n = 0, root = 0; double price = 0, rate = 0; cin >> n >> price >> rate; for (int i = 0,tem = 0; i < n; i++) { scanf("%d",&tem); if (tem == -1)root = i; else p[tem].push_back(i); } dfs(root,0); printf("%.2lf %d\n",price*pow(1+rate/100,level),cnt); system("pause"); return 0; }

 

最新回复(0)