1090 Highest Price in Supply Chain (25point(s)) Easy only once *1079雷同,层序遍历,layer计算

基本思想:

之前刷过的雷同题目;

 

关键点:

无;

 

#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include
using namespace std;

int cnt = 0;
int n;
double p;//初始价格
double r;//利率;
int root;
double max_num=0;

struct node {
	vectorchild;
	int layer;
};

vectortree;

void layer_init(int root) {
	if (tree.size() == 0)
		return;
	tree[root].layer = 0;
	queueq;
	q.push(root);
	while (!q.empty()){
		int index = q.front();
		q.pop();
		for (int i = 0; i < tree[index].child.size(); i++) {
			tree[tree[index].child[i]].layer = tree[index].layer+1;
			q.push(tree[index].child[i]);
		}
	}
}

void find(int root) {
	if (tree[root].child.size() == 0) {
		//如果为叶子节点;
		double value = p * pow(1 + 0.01*r, tree[root].layer);
		if (value > max_num) {
			max_num = value;
			cnt = 1;
		}
		else if (value == max_num) {
			cnt++;
		}
	}
	for (int i = 0; i < tree[root].child.size(); i++) {
		find(tree[root].child[i]);
	}
}

int main() {
	cin >> n >> p >> r;
	int a;
	tree.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> a;
		if (a == -1)
			root = i;
		else
			tree[a].child.push_back(i);
	}
	layer_init(root);
	find(root);
	printf("%.2lf %d", max_num, cnt);
	return 0;
}

  

你可能感兴趣的:(1090 Highest Price in Supply Chain (25point(s)) Easy only once *1079雷同,层序遍历,layer计算)