统计树的最小层数以及位于该层数上的叶子节点个数即可。

  代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用:

  http://www.cnblogs.com/chenxiwenruo/p/4513754.html

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/*
计算最小的层数与该层数上的叶子节点数目即可
建树用的是链式前向星模板,不了解的参考:
http://www.cnblogs.com/chenxiwenruo/p/4513754.html
*/
const int maxn=+;
int head[maxn];
int tot=;
int minlayer=maxn;
int cnt=;
struct Edge{
int to,next;
}edge[maxn];
void add(int x,int y){
edge[tot].next=head[x];
edge[tot].to=y;
head[x]=tot++;
}
void init(){
memset(head,-,sizeof(head));
tot=;
} void dfs(int i,int layer){
if(head[i]==-){
if(layer<minlayer){
cnt=;
minlayer=layer;
}
else if(layer==minlayer){
cnt++;
}
return;
}
for(int k=head[i];k!=-;k=edge[k].next){
int v=edge[k].to;
dfs(v,layer+);
}
}
int main()
{
int n;
double p,r;
scanf("%d %lf %lf",&n,&p,&r);
int a,b;
init();
for(int i=;i<n;i++){
scanf("%d",&a);
for(int j=;j<a;j++){
scanf("%d",&b);
add(i,b);
}
}
dfs(,);
double sum=p;
for(int i=;i<minlayer;i++){
sum*=(+r/);
}
printf("%.4lf %d",sum,cnt);
return ;
}

PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)的更多相关文章

  1. PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...

  2. [建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)

    1106. Lowest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors( ...

  3. 1106. Lowest Price in Supply Chain (25)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  4. PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)

    简单dfs #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  5. 【PAT甲级】1106 Lowest Price in Supply Chain (25分)

    题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...

  6. PAT甲级——1106 Lowest Price in Supply Chain(BFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872 1106 Lowest Price in Supp ...

  7. PAT 甲级 1106 Lowest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply chain is a ne ...

  8. PAT 1106 Lowest Price in Supply Chain

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  9. PAT甲题题解-1006. Sign In and Sign Out (25)-找最小最大

    判断哪个人最早到,哪个人最晚走水,就是找最大值最小值 #include <iostream> #include <cstdio> #include <algorithm& ...

随机推荐

  1. SQLSERVER无排序生成序号

    实现方式:ROW_NUMBER() SELECT RowID=(ROW_NUMBER() OVER(ORDER BY(SELECT ))) FROM dbo.tbl_name 实现方式:IDENTIT ...

  2. win10管理员已阻止你运行此应用”解决方法

    方法/步骤 1 按WIN+R键,打开“运行”,然后输入“gpedit.msc",就是打开组策略,这个在控制面板中也可以打开. 2 在组策略里找到“计算机配置”-“Windows设置”-“安全 ...

  3. windows下mysql和linux下mysql主从配置

    1. linux下mysql安装版本5.6   windows下mysql版本5.7  不要问我为什么版本不一致  就是想这么搞 2. linux为主服务器   windows为从服务器 3.找到li ...

  4. php 两个二维数组重组新数组,数组下标不同

    Array ( [0] => Array ( [PosNum] => 27025008 [start_time] => 20180328164929 [type] => 0 ) ...

  5. Oracle RMAN 恢复数据库到不同主机(一)

    一.RMAN 备份的内容 RMAN做数据库全备时包含了 数据文件.归档日志.控制文件和参数文件和备份日志,如下: arch_20160223_08qukp2t_1_1  arch_20160223_0 ...

  6. WorldWind源码剖析系列:大气层散射球类AtmosphericScatteringSphere

    大气层散射球类AtmosphericScatteringSphere代表地球外面的大气层散射球模型,可以控制开启/关闭渲染该对象.该类的类图如下. 该类所包含的主要字段.属性和方法如下: public ...

  7. Python2.7-io

    io 模块,用于处理流数据,在 python2 下,是内置 file 对象的另一种选择,在 python3 下,此模块是默认的文件和流数据的接口. 1.模块继承关系 IOBase--RawIOBase ...

  8. mysql安装,oracle安装

    mysql 版本:5.5.20 直接是是是装完, 密码设为123456, 检查服务, 然后装navicat 32位,64位均可,连接时输入root,123456. 连接成功!为所欲为操作数据库. ht ...

  9. 接收键盘输入的字符串,用FileWirter类将字符串写入文件,用FileReader类读出文件内容显示在屏幕上

    public class SY63 { public static void main(String[] args) throws Exception { System.out.print(" ...

  10. array_multisort函数,以及多维数组下排序的应用,并与usort函数对比

    以前比较少用这个函数,大部分自己接触的业务里,处理稍微大一些的数组的时候几乎都是从db里取出来的,在db里就order by了. 最近倒是用了次,这个函数用来排序很强大,有点类似于sql中的order ...