PAT A1090 Highest Price in Supply Chain(25 分)--树---dfs简单应用

总结:

1.这道题是dfs简单运用,map简单应用,读懂题意不粗心即可,加油加油~~~~~~~~~~~~~~~~~~~~~~~~~~~~

代码

#include<iostream>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
const int maxn = 100010;
int num, root;
double r, pr;
vector<vector<int> > pp;
double max1 = -1;
map<double, int> sg;
void dfs(int index, int high)
{
    double price = -1;
    if (pp[index].size() != 0)
    {
        for (int i = 0; i < pp[index].size(); i++)
            dfs(pp[index][i], high + 1);
    }
    else
    {
        price = pr*pow(1 + r, high);
        if (price>max1){ max1 = price; sg[max1] = 1; }
        else if (price == max1){ sg[max1]++; }
    }
}
int main()
{
    cin >> num >> pr >> r;
    pp.resize(num);
    r /= 100;
    for (int i = 0; i < num; i++)
    {
        int sk;
        cin >> sk;
        if (sk == -1)root = i;
        else{
            pp[sk].push_back(i);
        }
    }
    dfs(root, 0);
    printf("%.2f %d", max1,sg[max1]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/81986181