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

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

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=100000+5;
int product[maxn];
int head[maxn];
int tot=0;
double sum=0,p,r;
int cnt=0;
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,-1,sizeof(head));
    tot=0;
}

void dfs(int i,int layer){
    if(head[i]==-1){
        sum+=product[i]*p*pow(1+r/100,layer);
        return;
    }
    for(int k=head[i];k!=-1;k=edge[k].next){
        int v=edge[k].to;
        dfs(v,layer+1);
    }
}
int main()
{
    int n;
    scanf("%d %lf %lf",&n,&p,&r);
    int a,b;
    memset(product,0,sizeof(product));
    init();
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        if(a==0){
            scanf("%d",&product[i]);
        }
        for(int j=0;j<a;j++){
            scanf("%d",&b);
            add(i,b);
        }
    }
    dfs(0,0);
    printf("%.1lf",sum);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/chenxiwenruo/p/6390154.html