1090 Highest Price in Supply Chain(25分)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one's supplier in a pricePand sell or distribute them in a price that isr% higher thanP. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.
Input Specification:
Each input file contains one test case. For each case, The first line contains three positive numbers:N(≤105), the total number of the members in the supply chain (and hence they are numbered from 0 toN−1);P, the price given by the root supplier; andr, the percentage rate of price increment for each distributor or retailer. Then the next line containsNnumbers, each number Si
is the index of the supplier for the i-th
member.Sroot
for the root supplier is defined to be−1. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed1010.
Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2
注意点
- 对于树的深度的处理:如果找到一个大于当前深度的结点,保存当前深度,并将原先的当前深度的结点个数之一;当相等时,累加即可。当时没有想到,尽管现在看起来不难(o(╥﹏╥)o)
- 一句话的错误理解:
Si
is the index of the supplier for thei-th
member
翻译:对于第i个树来说,si是其供应商(即父节点)的编号。
代码
- 第一次
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct node
{
vector<int> child;
}Node[maxn];
int n, sum;
double p, r;
double ans;
int ansDepth[maxn];
void dfs(int index, int depth)
{
if (Node[index].child.size() == 0)
{
ansDepth[depth] ++;
return;
}
for (int i = 0; i < Node[index].child.size(); i ++)
{
dfs(Node[index].child[i], depth + 1);
}
}
int main()
{
scanf("%d %lf %lf", &n, &p, &r);
int id;
int root;
for (int i = 0; i < n; i ++)
{
scanf("%d", &id);
if (id == -1)
{
root = i;
}
else
{
Node[id].child.push_back(i);
}
}
dfs(root, 0);
int num = -1;
for (int i = 0; i < n ;i ++)
{
if (ansDepth[i] != 0)
{
if (i > num)
{
num = i;
}
}
}
ans = p * pow(1 + r / 100, num);
printf("%.2lf %d", ans, ansDepth[num]);
return 0;
}
- 改进
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
const int maxn = 100010;
struct node
{
vector<int> child;
}Node[maxn];
int n, sum;
double p, r;
int ansDepth = -1, num = 0;
void dfs(int index, int depth)
{
if (Node[index].child.size() == 0)
{
if (depth > ansDepth)
{
ansDepth = depth;
num = 1;
}
else if (depth == ansDepth)
{
num ++;
}
return;
}
for (int i = 0; i < Node[index].child.size(); i ++)
{
dfs(Node[index].child[i], depth + 1);
}
}
int main()
{
scanf("%d %lf %lf", &n, &p, &r);
int id;
int root;
for (int i = 0; i < n; i ++)
{
scanf("%d", &id);
if (id == -1)
{
root = i;
}
else
{
Node[id].child.push_back(i);
}
}
dfs(root, 0);
double ansPrice = p * pow(1 + r / 100, ansDepth);
printf("%.2lf %d", ansPrice, num);
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。