模板题目
DFS:
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1e5 + 10;
struct node {
double p;
vector<int>child;
}Node[maxn];
double minp = 1e10 + 10;
int cnt = 0;
int n;
double p, r;
void DFS(int root)
{
if (Node[root].child.size() == 0)
{
if (Node[root].p < minp)minp = Node[root].p, cnt = 1;
else if (Node[root].p == minp)cnt++;
return;
}
for (int i = 0; i < Node[root].child.size(); i++)
{
int child = Node[root].child[i];
Node[child].p = Node[root].p*(1 + r);
DFS(child);
}
}
int main()
{
scanf("%d%lf%lf", &n, &p, &r);
r /= 100;
for (int i = 0; i < n; i++)
{
int k, x;
scanf("%d", &k);
while (k--)
{
scanf("%d", &x);
Node[i].child.push_back(x);
}
}
Node[0].p = p;
DFS(0);
printf("%.4f %d", minp, cnt);
return 0;
}
BFS:
void BFS(int root)
{
queue<int>q;
q.push(root);
while (!q.empty())
{
int top = q.front();
q.pop();
if (Node[top].child.size())
{
for (int i = 0; i < Node[top].child.size(); i++)
{
int child = Node[top].child[i];
Node[child].p = Node[top].p*(1 + r);
q.push(child);
}
}
else
{
if (Node[top].p < minp)minp = Node[top].p, cnt = 1;
else if (Node[top].p == minp)cnt++;
}
}
}
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
vector<int>a[maxn];
int n;
double P,r;
int minheight=maxn,ans;
void DFS(int v,int height)
{
if(a[v].size()==0)
{
if(height<minheight)minheight=height,ans=1;
else if(height==minheight)ans++;
}
for(int i=0;i<a[v].size();i++)
{
int u=a[v][i];
DFS(u,height+1);
}
}
int main()
{
scanf("%d%lf%lf",&n,&P,&r);
r/=100;
for(int i=0;i<n;i++)
{
int k,x;
scanf("%d",&k);
while(k--)
{
scanf("%d",&x);
a[i].push_back(x);
}
}
DFS(0,1);
printf("%.4f %d",P*pow(1+r,minheight-1),ans);
return 0;
}