pat 1090 DFS

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

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

int N;
double originP,addP;

typedef struct
{
    vector son;
}NODE;

int dad[100005];
int root;
NODE node[100005];
double maxP = 0;
int maxId = 0,maxNum = 0;

void dfs(int n,double price)
{
    int sum = node[n].son.size();
    for(int i = 0;i < sum;i++)
    {
        int t = node[n].son[i];
        double addPrice = price + price*0.01*addP;
        dfs(t,addPrice);
    }
    if(price > maxP)
    {
        maxP = price;
        maxId = n;
        maxNum = 0;
    }
    if(price == maxP) maxNum++;
}

int main()
{
    cin >> N >> originP >> addP;
    for(int i =0 ;i < N;i++)
    {
        int t;
        cin >> t;
     //   dad[i] = t;
        node[t].son.push_back(i);
        if(t == -1) root = i;
    }
    dfs(root,originP);
    printf("%.2lf %d\n",maxP,maxNum);

    return 0;
}

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