PAT.A1090 Highest Price in Supply Chain

Back to Contents

Insert picture description here

Title

Given a tree for sale and supply, the roots are unique. The root price of the tree is p, and then starting from the root node, the price goes up by r% for each lower layer. Find the highest price among all leaf nodes and the number of leaf nodes with the highest price.

Sample (can be copied)

9 1.80 1.00
1 5 4 4 -1 4 5 3 6
//output
1.85 2

important point

  1. This question is similar to the above question A1079, DFS and BFS can be used, because the upper body uses DFS, this question uses BFS writing
  2. This question uses BFS, you can not use static trees or arrays to store node information, because you need to traverse the node information once when looking for the child node, it will time out, this question uses map <int, set> node; store node information, you can also consider using 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;
}
Published 177 original articles · won praise 5 · Views 6670

Guess you like

Origin blog.csdn.net/a1920993165/article/details/105499485