题目详情:https://www.patest.cn/contests/pat-a-practise/1106
提交情况:
提交代码:
#include <iostream>
#include <string.h>
#include <vector>
#include <math.h>
using namespace std;
#define Max 100010
#define INF 0x3f3f3f3f
vector<int> child[Max];
int root[Max],Root,depth,visit[Max],n,depths[Max];
double rootPrice,percent;
void DFS( int vertex ,int depth )
{
visit[vertex] = 1;
depths[vertex] = depth; //得到节点的深度
for( int i=0;i<child[vertex].size();++i )
{
int key = child[vertex][i];
if( visit[key] == 0 )
{
++depth; //跟踪节点的深度
DFS(key,depth);
--depth;
}
}
}
int main()
{
cin>>n>>rootPrice>>percent;
for( int i=0;i<n;++i ) //输入处理
{
int num;
cin>>num;
for( int j=0;j<num;++j )
{
int kid;
cin>>kid;
child[i].push_back(kid);
}
}
// cout<<"Root is "<<Root<<endl;
depth = 1;
DFS(0,depth);
int minDepth = INF,number = 0;
for( int i=0;i<n;++i ) //得到最大的深度,并用number记录最大深度的个数
{
if( child[i].size()==0 && depths[i] < minDepth )
{
minDepth = depths[i];
number = 1;
}
else if ( child[i].size()==0 && depths[i] == minDepth )
{
++number;
}
//以下代码查看各节点的深度
// if( i == n-1 )
// cout<<depths[i]<<endl;
// else
// cout<<depths[i]<<" ";
}
printf("%.4lf %d",rootPrice*pow(1+percent/100,minDepth-1),number);
return 0;
}
最什么好说,和1079题,1090题都差不多,一个求最高的售价,最低的售价和总的销售额。本题求最低售价,1090题求最高售价,1079求总的销售额。