疑问
暂无
代码
//注意精度
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 100010;
struct node{
int depth;
int num;
vector<int> child;
}Node[maxn];
//数量
int c=0;
//输入结点数
int n=0;
//原始价格
double p=0;
//利润
double r=0;
//总价格
double sale=0;
//最小深度
int minDepth=maxn;
void preOrder(int root,int depth){
Node[root].depth = 1+depth;
if(Node[root].child.size() == 0){
if(Node[root].depth < minDepth){
minDepth = Node[root].depth;
c = 1;
}else if(Node[root].depth == minDepth){
c++;
}
return;
}else{
for(int i=0;i<Node[root].child.size();i++){
preOrder(Node[root].child[i],Node[root].depth);
}
return;
}
}
int main(){
scanf("%d %lf %lf",&n,&p,&r);
r = r/100;
for(int i=0;i<n;i++){
int num;
scanf("%d",&num);
if(num != 0){
for(int j=0;j<num;j++){
int child;
scanf("%d",&child);
Node[i].child.push_back(child);
}
}
}
preOrder(0,-1);
sale = p * pow((1+r),minDepth);
printf("%.4f %d",sale,c);
return 0;
}
反思
- 相比之前的A1190,我在这里参考了《算法笔记》上使用全局变量
num
的方法来实时记录最小深度的数量; - 这里依然可以使用最简单的树的表达形式:
vector<int> Node[maxn]
;