用vector数组来存储每个点的子节点编号,然后通过广度优先搜索从根结点开始,来寻找最小的深度的根,并统计个数。
AC代码:
#include<iostream>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#include<hash_map>
#define ll long long
#define inf 24*60*60
using namespace std;
struct node
{
int id;
int lay=0;
};
vector<node> v[100000];
int mark=100000000;
int ans=0;
void bfs(int x)
{
queue<node> q;
node tmp;
tmp.id=0;
q.push(tmp);
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.lay>mark)
{
break;
}
if(v[t.id].empty())
{
mark=t.lay;
ans++;
continue;
}
for(int i=0;i<v[t.id].size();i++)
{
v[t.id][i].lay=t.lay+1;
q.push(v[t.id][i]);
}
}
}
int main()
{
int n;
double p,r;
scanf("%d %lf %lf",&n,&p,&r);
for(int i=0;i<n;i++)
{
int k;
scanf("%d",&k);
for(int j=0;j<k;j++)
{
node tmp;
scanf("%d",&tmp.id);
v[i].push_back(tmp);
}
}
bfs(0);
printf("%.4lf %d",p*pow(1+r/100,mark),ans);
}