PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
统计树的最小层数以及位于该层数上的叶子节点个数即可。
代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用:
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计算树的最小层数)的更多相关文章
- PAT Advanced 1106 Lowest Price in Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- [建树(非二叉树)] 1106. Lowest Price in Supply Chain (25)
1106. Lowest Price in Supply Chain (25) A supply chain is a network of retailers(零售商), distributors( ...
- 1106. Lowest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
简单dfs #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- 【PAT甲级】1106 Lowest Price in Supply Chain (25分)
题意:输入一个正整数N(<=1e5),两个小数P和R,分别表示树的结点个数和商品原价以及每下探一层会涨幅的百分比.输出叶子结点深度最小的商品价格和深度最小的叶子结点个数. trick: 测试点1 ...
- PAT甲级——1106 Lowest Price in Supply Chain(BFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90444872 1106 Lowest Price in Supp ...
- PAT 甲级 1106 Lowest Price in Supply Chain
https://pintia.cn/problem-sets/994805342720868352/problems/994805362341822464 A supply chain is a ne ...
- PAT 1106 Lowest Price in Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- PAT甲题题解-1006. Sign In and Sign Out (25)-找最小最大
判断哪个人最早到,哪个人最晚走水,就是找最大值最小值 #include <iostream> #include <cstdio> #include <algorithm& ...
随机推荐
- Alpha冲刺报告(4/12)(麻瓜制造者)
今日完成的情况 江郑: 今天对数据库的需求部分进行了完善 邓弘立: 完成了首页界面UI 刘双玉: 基本完成商品信息发布接口 汪志彬: 尝试UI的设计 符天愉: 将登录接口部署到服务器上,结果Linux ...
- buffers与cached
下面是buffers与cached的区别. buffers是指用来给块设备做的缓冲大小,他只记录文件系统的metadata以及 tracking in-flight pages. cached是用来给 ...
- python第三十九课——面向对象(二)之设计类
1.设计类class 车: #属性 颜色 = red 品牌 = "BMW" 车牌 = "沪A88888" #函数 行驶(): 停止(): 2.实例化车对象 ca ...
- Python在自动化运维时经常会用到的方法
得到当前工作目录,即当前Python脚本工作的目录路径:os.getcwd() 返回指定目录下的所有文件和目录名:os.listdir() 函数用来删除一个文件:os.remove() 删除多个目录: ...
- BZOJ3211:花神游历各国(线段树)
Description Input Output 每次x=1时,每行一个整数,表示这次旅行的开心度 Sample Input 4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 ...
- 恶意代码技术第一二部分——P201421410029
学 号:201421410029 中国人民公安大学 Chinese people’ public security university 网络对抗技术 实验报告 实验四 恶意代码技术 ...
- OC4J Configuration issue. /u01...dbhome_1/oc4j/j2ee/OC4J_DBConsole_orcl-db-01_orcl not found.
emctl start dbconsole 报错信息: OC4J Configuration issue. /u01/app/Oracle/product/11.2.0/dbhome_1/oc4j/j ...
- android中如何获取指定目录下的图片
需要对指定目录的图片文件进行列表,借鉴了网上的方法,发现列表出来是所有的文件,这样用起来很不方便,在这里也没找到解决的办法,经过自己的进一步研究终于搞定,发上来给有用的同学.用下面这种方式能实现查询实 ...
- /etc/hosts,GoldenGate
[oracle@g]$ netstat -alp|grep 7809(Not all processes could be identified, non-owned process info wil ...
- GDI+编程小结
GDI+(Graphics Device Interface Plus图形设备接口加)是Windows XP和Windows Server 2003操作系统的子系统,也是.NET框架的重要组成部分,负 ...