PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)

时间:2023-03-09 03:45:53
PAT (Advanced Level) 1090. Highest Price in Supply Chain (25)

简单dfs。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std; const int maxn=+;
vector<int>g[maxn];
int n;
double p,r;
int root;
int ans_x;
double ans_price=; void dfs(int x,double price)
{
if(g[x].size()==)
{
if(price>ans_price)
{
ans_price=price;
ans_x=;
}
else if(price==ans_price)
{
ans_x++;
}
return;
} for(int i=;i<g[x].size();i++)
{
dfs(g[x][i],price*(1.0+r/));
}
} int main()
{
scanf("%d",&n);
scanf("%lf%lf",&p,&r); for(int i=;i<n;i++)
{
int fa; scanf("%d",&fa);
if(fa==-) root=i;
else g[fa].push_back(i);
} dfs(root,p); printf("%.2lf %d\n",ans_price,ans_x); return ;
}