#1090. Highest Price in Supply Chain【树的遍历 + 记忆化搜索】

原题链接

Problem Description:

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 price P P P and sell or distribute them in a price that is r r r% higher than P P P. 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 N N ( ≤ 1 0 5 \leq 10^5 105), the total number of the members in the supply chain (and hence they are numbered from 0 to N − 1 N−1 N1); P P P, the price given by the root supplier; and r r r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N N N numbers, each number S i S_i Si is the index of the supplier for the i i i-th member. S r o o t S_{root} Sroot​ for the root supplier is defined to be − 1 −1 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 exceed 1 0 10 10^{10} 1010.

Sample Input:

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

Sample Output:

1.85 2

Problem Analysis:

根据题意,我们可以模拟样例如下:
在这里插入图片描述
可见,零售商表示叶子节点,每个零售商的售卖商品件数不同,并且售卖的价格取决于该叶子节点到根节点的距离。

我们可以采用记忆化搜索的方式来处理每个点到根节点的距离,这样可以省去建树的步骤。

当然也可以直接建树然后从根节点开始遍历,并且处理每个节点的深度。

Code

建树 + DFS

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;

const int N = 1e5 + 10;

int n;
int h[N], e[N], ne[N], idx;
int f[N];
int max_depth, cnt;
double P, R;

void add(int a, int b)
{
    
    
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs(int u, int depth)
{
    
    
    f[u] = depth;
    max_depth = max(max_depth, depth);
    for (int i = h[u]; ~i; i = ne[i])
    {
    
    
        int j = e[i];
        dfs(j, depth + 1);
    }
}

int main()
{
    
    
    cin >> n >> P >> R;

    memset(h, -1, sizeof h);
    int root = -1;
    for (int i = 0; i < n; i ++ ) 
    {
    
    
        int father;
        scanf("%d", &father);
        if (father == -1) root = i;
        add(father, i);
    }    

    dfs(root, 0);
    for (int i = 0; i < n; i ++ )
        if (f[i] == max_depth)
            ++ cnt;

    printf("%.2f %d\n", P * pow(1 + R / 100, max_depth), cnt);

    return 0;
}

记忆化搜索

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 1e5 + 10;

int n;
int f[N], p[N];
double P, R;

int dfs(int u)
{
    
    
    if (f[u] != -1) return f[u];
    if (p[u] == -1) return 0;

    return f[u] = dfs(p[u]) + 1;
} 

int main()
{
    
    
    cin >> n >> P >> R;
    for (int i = 0; i < n; i ++ ) cin >> p[i];

    memset(f, -1, sizeof f);

    int res = 0, cnt = 0;
    for (int i = 0; i < n; i ++ )
        if (dfs(i) > res)
        {
    
    
            res = dfs(i);
            cnt = 1;
        }
        else if (dfs(i) == res) ++ cnt;

    printf("%.2lf %d\n", P * pow(1 + R / 100, res), cnt);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121025668