简单dfs。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; const int maxn=100000+10; vector<int>g[maxn]; int n; double p,r; int root; int ans_x; double ans_price=0; void dfs(int x,double price) { if(g[x].size()==0) { if(price>ans_price) { ans_price=price; ans_x=1; } else if(price==ans_price) { ans_x++; } return; } for(int i=0;i<g[x].size();i++) { dfs(g[x][i],price*(1.0+r/100)); } } int main() { scanf("%d",&n); scanf("%lf%lf",&p,&r); for(int i=0;i<n;i++) { int fa; scanf("%d",&fa); if(fa==-1) root=i; else g[fa].push_back(i); } dfs(root,p); printf("%.2lf %d ",ans_price,ans_x); return 0; }