建树,然后从根节点dfs
注意考虑只有根节点的情况,否则测试点3过不去。(将maxdeep初始化为0,ans2初始化为1,此时输出 P 1)
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
struct node{
int cl_num=0;
vector<int> cl;
}Node[100000];
int N,root,maxdeep=0,ans2=1;
double P,r;
void dfs(int index,int deep){
if(Node[index].cl_num==0) return;
deep++;
if(deep>maxdeep){
maxdeep=deep;
ans2=Node[index].cl_num;
}else if(deep==maxdeep){
ans2+=Node[index].cl_num;
}
for(auto it=Node[index].cl.begin();it!=Node[index].cl.end();it++){
dfs(*it,deep);
}
}
int main(){
cin>>N>>P>>r;
for(int i=0;i<N;i++){
int index;
scanf("%d",&index);
if(index==-1){
root=i;
continue;
}
Node[index].cl.push_back(i);
Node[index].cl_num++;
}
dfs(root,0);
printf("%.2f %d",P*pow(1+r/100,maxdeep),ans2);
return 0;
}