PAT甲级 1106 Lowest Price in Supply Chain (25 分)

\quad 树的层次遍历,需记录每一层零售商数量。若某一层出现零售商,即该曾出现叶子节点,则统计该层叶子节点个数,计算出结果输出即可。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e+5+10;
vector<int> v[maxn];

int main(int argc, char const *argv[])
{
	int n;
	double p, r;
	scanf("%d%lf%lf", &n, &p, &r);
	r = r/100.0;
    for (int i = 0; i < n; ++i)
    {
    	int num;
    	scanf("%d", &num);
    	int temp;
    	for (int j = 0; j < num; ++j)
    	{
    		scanf("%d", &temp);
    		v[i].push_back(temp);
    	}
    }

    queue<int> q;
    q.push(0);
    int level = 0;
    int count = 0;
    q.push(-1);
    while(!q.empty())
    {
    	int t = q.front();
    	q.pop();
    	if(t==-1) break;
    	if(v[t].size()==0)  // 当只有供应商的时候供应商算零售商,即只有root节点时该节点算零售商节点
    	{
    		count++;
    	}
    	for (int i = 0; i < v[t].size(); ++i)
    	{
    		q.push(v[t][i]);
    	}
    	if(q.front()==-1)
    	{
    		if(count!=0)
    		{
    			printf("%.4f %d\n", p*pow(1+r, level), count);
    			break;
    		}
    		level++;
    		q.pop();
    		count = 0;
    		q.push(-1);
    	}
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40438165/article/details/90240749