题意:
题意的理解需要注意
each number Si is the index of the supplier for the i-th member.意思是说所给的数值依次是第i的供应商,也就是父类,此处的i是从0到n-1,并不是说所给的数值,所给的数值为Si,i并没有直接给出。
the number of retailers that sell at the highest price.这是输出中的一句话,指价格最高零售商的数量,并不是说编号。
解答:
这是一个求树的深度的题目,以为题目中给了每个儿子的父亲,所以直接递归求深度即可。同时,还需要记录最大深度的个数。同时注意输出格式和Math.pow(a,b)的应用。
具体java代码:
import java.util.Scanner;
public class Main{
public static int[] father;
public static int[] level;
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int N=in.nextInt();
float p=in.nextFloat();
float r=in.nextFloat();
father=new int[N];
for(int i=0;i<N;i++){
father[i]=in.nextInt();
}
in.close();
level=new int[N];
for(int i=0;i<N;i++){
level[i]=-1;
}
int maxLevel=0;
int num=0;
for(int i=0;i<N;i++){
int temp=getLevel(i);
if(temp>maxLevel){
maxLevel=temp;
num=1;
}
else if(temp==maxLevel)
num++;
}
System.out.printf("%.2f %d",p*(Math.pow(1+r/100,maxLevel)),num);
}
public static int getLevel(int i){
if(father[i]==-1) return 0;
if(level[i]<0)
level[i]=getLevel(father[i])+1;
return level[i];
}
}