PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
2016-11-30 18:40
513 查看
统计树的最小层数以及位于该层数上的叶子节点个数即可。
代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用:
http://www.cnblogs.com/chenxiwenruo/p/4513754.html
View Code
代码里建树我用了邻接链表的存储方式——链式前向星,不了解的可以参考,非常好用:
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=100000+5; int head[maxn]; int tot=0; int minlayer=maxn; int cnt=0; 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,-1,sizeof(head)); tot=0; } void dfs(int i,int layer){ if(head[i]==-1){ if(layer<minlayer){ cnt=1; minlayer=layer; } else if(layer==minlayer){ cnt++; } return; } for(int k=head[i];k!=-1;k=edge[k].next){ int v=edge[k].to; dfs(v,layer+1); } } int main() { int n; double p,r; scanf("%d %lf %lf",&n,&p,&r); int a,b; init(); for(int i=0;i<n;i++){ scanf("%d",&a); for(int j=0;j<a;j++){ scanf("%d",&b); add(i,b); } } dfs(0,0); double sum=p; for(int i=0;i<minlayer;i++){ sum*=(1+r/100); } printf("%.4lf %d",sum,cnt); return 0; }
View Code
相关文章推荐
- 1106. Lowest Price in Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)
- PAT (Advanced Level) Practise 1106 Lowest Price in Supply Chain(25)
- PAT 甲级 1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25) dfs
- pat 1106. Lowest Price in Supply Chain (25)
- pat 1106. Lowest Price in Supply Chain (25)
- PAT 1106. Lowest Price in Supply Chain (25) 单入度树 + 求叶子到树的距离+反向递归
- 1106. Lowest Price in Supply Chain (25) PAT甲级
- PAT甲级真题及训练集(26)--1106. Lowest Price in Supply Chain (25)
- Pat(A) 1106. Lowest Price in Supply Chain (25)
- PAT (Advanced Level) Practise 1106 Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25) 树的遍历,DFS
- 【PAT】【Advanced Level】1106. Lowest Price in Supply Chain (25)
- 1106. Lowest Price in Supply Chain (25) DFS,BFS
- PAT 1106. Lowest Price in Supply Chain (25)
- PAT_A 1106. Lowest Price in Supply Chain (25)
- 【PAT】1106. Lowest Price in Supply Chain (25)
- PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
- PAT - 甲级 - 1106. Lowest Price in Supply Chain (25)(多叉树)
- PAT 1106 Lowest Price in Supply Chain