PAT-练习集-L2-020. 功夫传人

本文介绍了一道关于树形图搜索的经典算法题,通过深度优先搜索(DFS)算法来计算得道者最终获得的总功力。文章详细解释了题目背景及解题思路,并给出了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//传送门: https://www.patest.cn/contests/gplt/L2-020
#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
using namespace std;
/*题意:
    简单的DFS树形图搜索题。
    根为祖师爷,
    向下搜索即可,
    以得道者和非得道者分开处理,
    再将得道者的功力求和。
    坑点:只有一个人,祖师爷即得道者
*/
#define N 100005
#define M 1000005
int n;
double z,r;
double dedao[N];  //存得道者的功力提升倍数,0 为非得道者
double ans[N];    //存答案
struct edge       //邻接表
{
   int v,next;
}e[M];
int head[N];
void dfs(int x)
{
   for(int i=head[x];~i;i=e[i].next){
      int v = e[i].v;
      if(dedao[v]){                       //如果是得道者,则不用向下递归
         ans[v] = ans[x] * r *  dedao[v];
      }else{
         ans[v] = ans[x] * r;              //非得道者
         dfs(v);
      }
   }
}

int main()
{
    scanf("%d%lf%lf",&n,&z,&r);
    r = 1. - r / 100.;                      // r需要预处理下才能使用
    int tot = 0;
    memset(head,-1,sizeof(head));
    if(n==1){                               // 若只有祖师爷一人,则其为得道者,1分坑点
      int a,b;
      scanf("%d%d",&a,&b);
      printf("%d",(int)floor(z*b));
      return 0;
    }
    for(int i=0;i<n;i++){
      int k;
      scanf("%d",&k);
      if(!k){                                 //得道者,记录功力提升倍数
      double a;
      scanf("%lf",&a);
      dedao[i] = a;
      }else{                                   //非得道者,记录其弟子
      for(int j=0;j<k;j++){
        int a;
        scanf("%d",&a);
      e[tot].v = a;
      e[tot].next = head[i];
      head[i] = tot++;
      }
      }
    }
    ans[0] = z;
    dfs(0);
    double tol = 0;                             //答案求和
    for(int i=0;i<n;i++){
      if(dedao[i]) tol += ans[i];
    }
    printf("%lld",(long long)floor(tol));       //题目保证输入和正确的输出都不超过10^10 用 int 也不会错
    return 0;
}



### 关于PAT L2-020 功夫传人的解题思路 此问题是关于树结构的遍历以及计算节点属性的一个典型应用。题目要求通过给定的数据构建一棵师徒关系树,并基于特定规则计算所有得道者的功力总值。 #### 数据处理与建模 输入数据提供了每个人的徒弟列表,因此可以将其视为一个图中的邻接表表示法。由于每个人都只有一个师父(除了祖师爷),这实际上是一个单向无环图(即树)。可以通过广度优先搜索 (BFS)[^2] 或深度优先搜索 (DFS)[^3] 来遍历该树。 #### 计算逻辑 1. **初始条件** 祖师爷的功力值 \( Z \) 是已知的,每一代弟子的功力会按照一定比例减少,具体由参数 \( r \% \) 控制。对于任意一个人 \( i \),其功力值可定义为: \[ P_i = P_{\text{master}} \times (\frac{r}{100})^{d} \] 其中 \( d \) 表示当前节点距离根节点的距离(代数)[^1]。 2. **特殊规则** 如果某个人被标记为“得道者”,则他们的功力会被放大一定的倍数。注意,这种放大的效果仅适用于他们本身,而不会传递给后代。最终的结果应直接取整而非四舍五入[^4]。 3. **算法选择** 可以采用 BFS 遍历来逐层访问每一个节点并更新相应的功力值。或者使用 DFS 进行递归求解,在回溯过程中累加符合条件的功力值[^5]。 #### 示例代码实现 以下是 Python 的一种可能实现方式: ```python import sys from collections import deque def main(): input_data = sys.stdin.read().splitlines() line_1 = list(map(float, input_data[0].split())) N, Z, R = int(line_1[0]), line_1[1], line_1[2]/100 children = [[] for _ in range(N)] ascended = [False]*N multipliers = [1.0]*N for idx in range(1, N+1): info = list(input_data[idx].split()) if info[0] != '0': child_list = list(map(int,info[1:])) children[idx-1] = child_list if len(info) >= 2 and info[-1][0]=='@': # Check last element is '@X' multiplier = float(info[-1][1:]) ascended[idx-1] = True multipliers[idx-1] = multiplier queue = deque([(0,Z)]) total_power = 0 while queue: current_node,power = queue.popleft() if ascended[current_node]: power *= multipliers[current_node] total_power += int(power) next_gen_power = power * R for child in children[current_node]: queue.append((child,next_gen_power)) print(total_power) if __name__ == "__main__": main() ``` 上述代码实现了利用队列完成层次遍历的过程,并依据是否为得道者调整了对应的功力值计算方法。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值