一、题目描述
Input Specification:
Output Specification:
Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2
二、解题思路
dfs的题目,相对简单。将每个输入的个体设为一个结构体,包括它的售价以及它所供应的对象。随后用dfs更新最大售价即可。与前面的一道Supply Chain的题非常类似,不再赘述。
三、AC代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int N, root, cnt = 0;
double price, r, maxp = 0;
struct Supply
{
double pr;
vector<int> child;
}sup[100010];
void dfs(int root)
{
if(sup[root].pr > maxp)
{
maxp = sup[root].pr;
cnt = 1;
}
else if(sup[root].pr == maxp) cnt++;
if(sup[root].child.size() == 0) return;
for(int i=0; i<sup[root].child.size(); i++)
{
sup[sup[root].child[i]].pr = sup[root].pr * r;
dfs(sup[root].child[i]);
}
}
int main()
{
scanf("%d%lf%lf", &N, &price, &r);
r = 1 + r/100;
int tmp;
for(int i=0; i<N; i++)
{
scanf("%d", &tmp);
if(tmp != -1) sup[tmp].child.push_back(i);
else
{
root = i;
sup[root].pr = price;
}
}
dfs(root);
printf("%.2f %d", maxp, cnt);
return 0;
}