题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944
做了1079以后,这道题就小意思了,一遍AC
代码
#include<algorithm>#include<vector>#include<iostream>#include<cmath>using namespace std;const int maxn = 100010;struct Node{vector<int> child;}node[maxn];//n是结点总数,p是根结点的供应商价格,r是每一层的增加百分比int n;double p, r, ans = 0;vector<double> ans_list;void DFS(int root, int depth){if(node[root].child.size() == 0){ans = p * pow(1 + r, depth);ans_list.push_back(ans);}else {for(int i = 0; i < node[root].child.size(); i++){DFS(node[root].child[i], depth + 1);}}}int main(){int id, root;scanf("%d%lf%lf", &n, &p, &r);r /= 100;for(int i = 0; i < n; i++){scanf("%d",&id);if(id == -1) root = i;else {node[id].child.push_back(i);}}DFS(root, 0);int max_num = 0;double max_value = -1;for(int i = 0; i < ans_list.size(); i++){if(ans_list[i] > max_value) max_value = ans_list[i];}for(int i = 0; i < ans_list.size(); i++){if(ans_list[i] == max_value) max_num++;}printf("%.2f %d", max_value, max_num);return 0;}
