1090 Highest Price in Supply Chain(25分)node
A supply chain is a network of retailers(零售商), distributors(經銷商), and suppliers(供應商)-- everyone involved in moving a product from supplier to customer.spa
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.code
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.three
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.ci
9 1.80 1.00 1 5 4 4 -1 4 5 3 6
1.85 2
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; }