1090 Highest Price in Supply Chain (25 分)

水~。

题意

给出一棵销售供应的树,树根唯一。在树根处货物的价格为P,然后从根结点开始每往子结点走一层,该层的货物价格将会在父亲结点的价格上增加r%。求所有叶结点中的最高价格以及这个价格的叶结点个数。

1079 Total Sales of Supply Chain (25 分)的简化题。

const int N=1e5+10;
vector<int> g[N];
int n;
double price,rate;
double ans;
int cnt;

void dfs(int root,double cost)
{
    if(g[root].size() == 0)
    {
        if(cost > ans)
        {
            ans=cost;
            cnt=1;
        }
        else if(cost == ans)
            cnt++;
        return;
    }

    for(int i=0;i<g[root].size();i++)
    {
        int j=g[root][i];
        dfs(j,cost*(1+rate));
    }
}

int main()
{
    cin>>n>>price>>rate;
    rate/=100;

    int root;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        if(~x) g[x].pb(i);
        else root=i;
    }

    dfs(root,price);

    printf("%.2f %d\n",ans,cnt);

    //system("pause");
    return 0;
}
posted @ 2021-02-26 23:05  Dazzling!  阅读(52)  评论(0编辑  收藏  举报