题目大意:
供应商只有一个,经销商进货通过供应商,零售商进货通过经销商或供应商,每个商家都用0~N-1编号标出,供应商是1,现在需要求出从零售商手里买到商品最高的价格和商家数。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int N,root;
double p, r;
vector<int> E[maxn];
int num = 0, maxLevel = -1;
void dfs(int v, int deep)
{if (E[v].size() == 0){if (maxLevel < deep){maxLevel = deep;num = 1;}else if (deep == maxLevel){num++;}}else {for (int i = 0; i < E[v].size() ; i++) {dfs(E[v][i], deep+1);}}
}int main()
{cin >> N >> p >> r;for (int i = 0; i <N; i++){int d;cin >> d;if(d==-1){root=i;}else{E[d].push_back(i);}}num = 0;dfs(root, 0);double ans = p;for (int i = 0; i < maxLevel; i++) {ans = ans*(1 + r*1.0/100.0);}printf("%.2f %d\n", ans, num);return 0;
}