构造一个二维数组 v ,v[i] 存放指向 i 的所有元素。
构造队列 q 存放每个待读取的节点。
构造数组 high 存放每个节点的高度(第几级经销商)
#include<iostream>
#include<queue>
#include<cmath>
using namespace std;
int main(){int n,temp,maxn=0,num=0;double p,r,ans;cin>>n>>p>>r;vector<int> v[n];queue<int> q;int high[n]={0};for(int i=0;i<n;i++){cin>>temp;if(temp==-1) q.push(i);else v[temp].push_back(i);}while(!q.empty()){temp=q.front();q.pop();for(int i=0;i<v[temp].size();i++){high[v[temp][i]]=high[temp]+1;q.push(v[temp][i]);}}for(int i=0;i<n;i++){if(high[i]>maxn){maxn=high[i];num=1;}else if(high[i]==maxn) num++;}printf("%.2f %d",p*pow(1.0+r/100.0,maxn),num);return 0;
}