1079. Total Sales of Supply Chain (25)-求数的层次和叶子节点

时间:2022-01-24 16:46:47

和下面是同类型的题目,只不过问的不一样罢了:

1090. Highest Price in Supply Chain (25)-dfs求层数

1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/*
计算最小的层数与该层数上的叶子节点数目即可
建树用的是链式前向星模板,不了解的参考:
http://www.cnblogs.com/chenxiwenruo/p/4513754.html
*/
const int maxn=+;
int product[maxn];
int head[maxn];
int tot=;
double sum=,p,r;
int cnt=;
struct Edge{
int to,next;
}edge[maxn];
void add(int x,int y){
edge[tot].next=head[x];
edge[tot].to=y;
head[x]=tot++;
}
void init(){
memset(head,-,sizeof(head));
tot=;
} void dfs(int i,int layer){
if(head[i]==-){
sum+=product[i]*p*pow(+r/,layer);
return;
}
for(int k=head[i];k!=-;k=edge[k].next){
int v=edge[k].to;
dfs(v,layer+);
}
}
int main()
{
int n;
scanf("%d %lf %lf",&n,&p,&r);
int a,b;
memset(product,,sizeof(product));
init();
for(int i=;i<n;i++){
scanf("%d",&a);
if(a==){
scanf("%d",&product[i]);
}
for(int j=;j<a;j++){
scanf("%d",&b);
add(i,b);
}
}
dfs(,);
printf("%.1lf",sum);
return ;
}