
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
using namespace std;
struct node
{
vector<int> children;
};
int n;
double p,r;
vector<node> C;
int cnt=1,min_depth=99999999;
void dfs(int root,int depth){//按层访问
if(depth>min_depth) return;//大于当前最小深度,则不必继续
if(C[root].children.size()==0){//叶结点
if(depth<min_depth){
min_depth=depth;
cnt=1;
}else if(depth==min_depth){
cnt++;
}
}
for(int i=0;i<C[root].children.size();i++){
dfs(C[root].children[i],depth+1);
}
}
int main(){
scanf("%d %lf %lf",&n,&p,&r);
C.resize(n);
int tn,t;
for(int i=0;i<n;i++){
scanf("%d",&tn);
if(tn>0){
while(tn--){
scanf("%d",&t);
C[i].children.push_back(t);
}
}
}
dfs(0,0);
printf("%.4f %d",p*pow(1+0.01*r,min_depth),cnt);
return 0;
}
本文介绍了一个使用深度优先搜索(DFS)算法遍历树形结构,并计算特定节点深度及出现次数的应用案例。通过C++实现,展示了如何定义树结构、进行深度优先遍历以及根据特定条件更新全局变量。
362

被折叠的 条评论
为什么被折叠?



