Pat1090 CodeTitle Description:
A supply chain is a network of retailers (retailer), distributors (dealer), and suppliers (supplier)--everyone involved in moving a produ CT from supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or Distribu Te them in a price, is r% higher than P. It is assumed this member in the supply chain have exactly one supplier except the root supplier, and there is no supp Ly cycle.
Now given a supply chain and you is supposed to tell the highest price we can expect from some retailers.
Input Specification:
Each input file contains the one test case. For each case, the first line contains three positive numbers:n (<=105), the total number of the Mem Bers in the supply chain (and hence they is numbered from 0 to N-1); P, the price of given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number S I was the index of the supplier for the i-th member. SRoot for the root supplier are defined to be-1. All the numbers in a line is separated by a space.
Output Specification:
for all test case, print on one line the highest price we can expect from some retailers, accurate up to 2 decimal pl Aces, and the number of retailers that sell at the highest. There must be one space between the numbers. It is guaranteed, the price would not be exceed ten.
Sample Input:
9 1.80 1.00
1 5 4 4-1 4 5 3 6
Sample Output:
1.85 2
AC Code:In fact, the height of the tree and the height of the number of leaf nodes, directly bfs on the line.
#include <cstdio> #include <cstdlib> #include <cmath> #include <vector> #include <deque># Define MAXN 100005using namespace Std;int n;int root;double p,r;int visited[maxn];vector<int> V[MAXN];d eque< int> q;void BFS (int root,int *height,int count[]) {int endnode;int maxlevel=-1;q.push_back (root); visited[root]=1; Endnode=q.back (), while (!q.empty ()) {int Index=q.front (), Q.pop_front (); for (int i=0;i<v[index].size (); i++) {int j= V[index][i];if (!visited[j]) {visited[j]=1;q.push_back (j);}} if (Index==endnode) {(*height) ++;//height of the Treecount[*height]=q.size ();//reserve the nodes of each levelendnode= Q.back ();}}} int main (int argc,char *argv[]) {int height=0,count[maxn];count[0]=1;scanf ("%d%lf%lf", &n,&p,&r); for (int i=0;i<n;i++) {int index;scanf ("%d", &index), if (index==-1) {root=i;} Else{v[index].push_back (i);}} BFS (Root,&height,count);p rintf ("%.2lf%d\n", P*pow ((1+r/100.0), height-1), count[height-1]); return 0;}
Pat (practice--1090) (highest price in supply Chain)