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

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于PAT (Advanced Level) 1079. Total Sales of Supply Chain (25)的主要内容,如果未能解决你的问题,请参考以下文章

PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]

PAT (Advanced Level) 1025. PAT Ranking (25)

PAT Advanced Level 1044

PAT Advanced Level 1043

PAT Advanced Level 1095

PAT Advanced Level 1038