一、思路
DFS遍历,到达底层进行价格分析。
注意根供应商直接给出价格,不以倍率r涨价。
二、代码
#include <cstdio>
#include <vector>
using namespace std;
void dfs( int node, double price, double r, double &Min, int &cnt, vector<vector<int>> &tree )
{
if( tree[node].size() )
price *= r;
else if( price > Min )
{
Min = price;
cnt = 1;
}
else if( price == Min )
++cnt;
for( int i = 0; i < tree[node].size(); ++i )
dfs( tree[node][i], price, r, Min, cnt, tree );
}
int main()
{
int N, root, cnt = 0;
double price, r, ans = 0;
scanf("%d %lf %lf", &N, &price, &r);
r = r / 100 + 1;
vector<vector<int>> tree(N);
for( int i = 0, node; i < N; ++i )
{
scanf("%d", &node);
if( node == -1 )
root = i;
else tree[node].push_back(i);
}
dfs( root, price, r, ans, cnt, tree );
printf("%.2f %d", ans, cnt);
}