PAT 1090 Highest Price in Supply Chain(25分)

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

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers:N(≤10​5​​), 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 S​i​​is the index of the supplier for the i-th member.S​root​​ for the root supplier is defined to be−1. All the numbers in a line are separated by a space.three

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 exceed10​10​​.ci

Sample Input:

9 1.80 1.00
1 5 4 4 -1 4 5 3 6

Sample Output:

1.85 2

注意點

  • 對於樹的深度的處理:若是找到一個大於當前深度的結點,保存當前深度,並將原先的當前深度的結點個數之一;當相等時,累加便可。當時沒有想到,儘管如今看起來不難(o(╥﹏╥)o)
  • 一句話的錯誤理解:
S​i​​is the index of the supplier for the i-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;
}
相關文章
相關標籤/搜索