题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944

做了1079以后,这道题就小意思了,一遍AC

代码

  1. #include<algorithm>
  2. #include<vector>
  3. #include<iostream>
  4. #include<cmath>
  5. using namespace std;
  6. const int maxn = 100010;
  7. struct Node{
  8. vector<int> child;
  9. }node[maxn];
  10. //n是结点总数,p是根结点的供应商价格,r是每一层的增加百分比
  11. int n;
  12. double p, r, ans = 0;
  13. vector<double> ans_list;
  14. void DFS(int root, int depth){
  15. if(node[root].child.size() == 0){
  16. ans = p * pow(1 + r, depth);
  17. ans_list.push_back(ans);
  18. }
  19. else {
  20. for(int i = 0; i < node[root].child.size(); i++){
  21. DFS(node[root].child[i], depth + 1);
  22. }
  23. }
  24. }
  25. int main(){
  26. int id, root;
  27. scanf("%d%lf%lf", &n, &p, &r);
  28. r /= 100;
  29. for(int i = 0; i < n; i++){
  30. scanf("%d",&id);
  31. if(id == -1) root = i;
  32. else {
  33. node[id].child.push_back(i);
  34. }
  35. }
  36. DFS(root, 0);
  37. int max_num = 0;
  38. double max_value = -1;
  39. for(int i = 0; i < ans_list.size(); i++){
  40. if(ans_list[i] > max_value) max_value = ans_list[i];
  41. }
  42. for(int i = 0; i < ans_list.size(); i++){
  43. if(ans_list[i] == max_value) max_num++;
  44. }
  45. printf("%.2f %d", max_value, max_num);
  46. return 0;
  47. }