PAT1079 DFS

https://pintia.cn/problem-sets/994805342720868352/problems/994805388447170560

#include
#include
#include
#include
using namespace std;

typedef struct
{
    vector son;
}NODE;

NODE node[100005];

int tree[100005];
int visit[100005];
int products[100005];
double ans = 0;
int N;
double originP,addP;
void dfs(int n,double price)
{
    if(visit[n] || n >= N) return;
    visit[n] = 1;
    int sum = node[n].son.size();
   // cout<< "   "<< n << ":" << sum << endl;
    for(int i = 0;i < sum;i++)
    {
        int son = node[n].son[i];
        double ap = price + price*addP*0.01;
        dfs(son,ap);
    }
    if(!sum) ans += price*products[n];
}

int main()
{

    cin >> N >> originP >> addP;
    for(int i =0 ;i < N;i++)
    {
        int n;
        cin >> n;
        if(!n)
        {
            int t;
            cin >> t;
            products[i] = t;
        }
        while(n--)
        {
            int t;
            cin >> t;
            node[i].son.push_back(t);
        }
    }
    dfs(0,originP);
    printf("%.1lf\n",ans);

    return 0;
}

你可能感兴趣的:(习题,PAT)