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

//传送门: 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;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值