https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944

``````#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
using std::vector;
using std::queue;
const int maxn=100010;
struct node{
double data;
vector<int>child;
int layer;
}Node[maxn];
int n;
double p,r;

bool cmp(node a,node b){
return a.layer>b.layer;
}

void BFS(int x){
int layer=-1;
queue<int>q;
q.push(x);
while(!q.empty()){
layer++;
int length=q.size();
for(int i=0;i<length;i++){
int index=q.front();
q.pop();
Node[index].layer=layer;
for(int j=0;j<Node[index].child.size();j++){
q.push(Node[index].child[j]);
}
}
}
}
int main(){
int root;
int mem;
scanf("%d%lf%lf",&n,&p,&r);
for(int i=0;i<n;i++){
scanf("%d",&mem);
if(mem!=-1){
//如果不是根节点
Node[mem].child.push_back(i);
}else{
root=i;
}
}
BFS(root);
sort(Node,Node+n,cmp);
int layer=Node[0].layer;
int sum=0;
for(int i=0;i<n;i++){
if(Node[i].layer==layer)
sum++;
}
printf("%.2f %d",p*pow(1+r/100,layer),sum);
system("pause");
return 0;
}

``````

1个回答

10 个月之前 回复

10 个月之前 回复