返回目录
题意
给出一颗销售供应的树,树根唯一。树根价格为p,然后从根节点开始,每向下一层,价格上涨r%。求所有叶节点中最高的价格和最高价格的叶节点个数。
样例(可复制)
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
//output
1.85 2
注意点
- 本题与上题A1079类似,使用DFS和BFS都可以,由于上体采用的是DFS,这题采用BFS的写法
- 本题使用BFS,不能用静态树或数组来存储节点信息,因为找孩子节点时需要遍历一遍节点信息,会超时,本题使用了map<int,set> node;存储节点信息,也可以考虑使用vector
#include<bits/stdc++.h>
using namespace std;
int n,tmp,maxdepth=0,maxnum=0;
double p,r;
map<int,set<int>> node;
void BFS(){
queue<int> q,depth;
q.push(-1);
depth.push(-1);
while(!q.empty()){
int now=q.front(),nowdep=depth.front();
q.pop();depth.pop();
if(nowdep==maxdepth)maxnum++;
if(nowdep>maxdepth){
maxdepth=nowdep;maxnum=1;
}
for(set<int>::iterator it=node[now].begin();it!=node[now].end();it++){
q.push(*it);depth.push(nowdep+1);
}
}
}
int main(){
cin>>n>>p>>r;
r/=100;
for(int i=0;i<n;i++){
scanf("%d",&tmp);
node[tmp].insert(i);
}
BFS();
printf("%.2f %d",p*pow(1+r,maxdepth),maxnum);
return 0;
}