题目描述
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a productfrom supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one'ssupplier in a price P and sell or distribute them in a price that is r% higherthan P.
It is assumed that each member in the supply chain has exactly one supplierexcept the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the highest price we canexpect from some retailers.
输入描述:
Each input file contains one test case. For each case, The first line contains threepositive numbers: N (<=105), the total number of the members in the supply chain (andhence they are numbered from 0 to N-1); P, the price given by the rootsupplier; and r, the percentage rate of price increment for each distributor orretailer. Then the next line contains Nnumbers, 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 areseparated by a space.
输出描述:
For each test case, print in one line the highest price we canexpect from some retailers, accurate up to 2 decimal places, and the number ofretailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.
输入例子:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
输出例子:
1.85 2
这一题做的很烦,题目重新理解了至少4才弄明白了这一题到底是什么意思
首先是这一句accurate upto 2 decimal places我开始以为是四舍五入,后来才想到这个应该是取两位小数,然后是这一句Sroot for the root supplier is defined to be -1我一直以为这一句话是说root的编号是-1,后来才发现这一句话的意思是指-1表示当前的节点正是root根节点,然后就是distribute them in a price that is r% higher than P,我一直以为是每个店家在前一个店家的基础上加上百分比,然后取两位小数,这样继续运算,实际上是直接加百分比,然后最后在取两位这样的,题目中并没有说每一步都要四舍五入,所以我们只需要在最后取两位小数就好了
看来英语阅读水平还优待提高
其实这一题就是一个简单的DFS,不知怎的我理解成了复杂的递归
另外还有一点就是当C++下输出double的时候,double的长度超过6位之后后面的数字不会显示,这个时候使用printf貌似就没有这个问题了,但是printf对于doube型的数据小数点之后会有6位,这个时候需要使用语句去控制。
以后要记住,double类型的数据不能直接等于号比较,使用大于小于也可能出现错误,最好是根据double的精度将其装换成long long类型来比较比较好,这样不容易出错,假如摸不准doube的可运算范围,可以写一个小程序验证一下,反正double 100亿上下的运算是精确的。
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
#define LL long long
double r;
int theDepthest=-1;
int N, theBestPNum=0;
void DFS(vector<vector<int>> &theEdges, int depth, int t)
{
if (depth > theDepthest) { theDepthest = depth; theBestPNum = 1; }
else if (depth == theDepthest) theBestPNum++;
for (int i = 0; i < theEdges[t].size(); i++)
{
DFS(theEdges, depth+1, theEdges[t][i]);
}
}
int main()
{
int tmp;
double prize;
int root;
cin >> N >> prize >> r;
r = 1.0 + r / 100;
vector<vector<int>> theEdges(N+1);
for (int i = 0; i < N; i++)
{
cin >> tmp;
if (tmp == -1) { root = i; continue; }
theEdges[tmp].push_back(i);
}
DFS(theEdges, 0, root);
for (int i = 0; i < theDepthest; i++)
{
prize = prize*r;
}
printf("%.2lf",prize);
cout << " " << theBestPNum;
return 0;
}