题目链接

深搜DFS

边搜索边记录

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
vector<int> v[maxn];
int dd[maxn]={0};
double ddz=0;
void dfs(int r,double cur, double c){
	
	if(dd[r] !=0 ){
		ddz += cur*dd[r];
		cur = cur*dd[r];
	}else{
		cur = cur * (1 - c/100.0); 
	}
	for(int i=0;i<v[r].size();i++){
		int j = v[r][i];
		dfs(j,cur,c);
	}
}
int main(){
	int n,k,num,tmp;
	double total,count;
	cin>>n>>total>>count;
	for(int i=0;i<n;i++){
		cin>>k;
		if(k==0){
			cin>>dd[i];
		}else{
			for(int j=0;j<k;j++){
				cin>>tmp;
				v[i].push_back(tmp);
			}
		}
	}
	dfs(0,total,count);
	printf("%d\n",int(ddz));
	return 0;
}