PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)

树的遍历。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn=100000+10;
vector<int>tree[maxn];
int n;
double P,r;
double sum;
int sz;
int num[maxn];

void dfs(int x,double price)
{
    if(tree[x].size()==0)
    {
        sum=sum+num[x]*price;
        sz++;
        return;
    }

    for(int i=0;i<tree[x].size();i++)
        dfs(tree[x][i],price*(1+r/100)*1.0);
}
int main()
{
    scanf("%d",&n);
    scanf("%lf%lf",&P,&r);
    for(int i=0;i<n;i++)
    {
        int ki; scanf("%d",&ki);

        if(ki==0)
        {
            int x; scanf("%d",&x);
            num[i]=x;
        }
        else
        {
            while(ki--)
            {
                int to; scanf("%d",&to);
                tree[i].push_back(to);
            }
        }
    }

    sum=0;sz=0;
    dfs(0,P);
    printf("%.1lf\n",sum);

    return 0;
}

 

posted @ 2016-07-02 22:32  Fighting_Heart  阅读(124)  评论(0编辑  收藏  举报