L2-020

据说点这里能进入修仙世界

解题思路

这道题唯一的难点是题意的理解,其中有几点非常关键:

  • 如果师傅的某个徒弟是得道者,那么 他获得的功力 \(=\) 师傅获得的功力 \(×\) 他的倍数

  • 得道者没有徒弟

吐槽一下,这道题太阴了,这个题目不好理解,而且从示例去推又不好推,因为涉及小数计算😠

依旧需要递归函数 , 递归的时候,要把师傅的功力传给徒弟,所以在函数里需要两个参数,一个代表自己的编号,在函数里要遍历每个师傅的徒弟(显然又是邻接表(#^.^#)),另一个代表师傅的功力,要通过一定方式给徒弟

用一个times数组存倍数,如果是得道者就输入times,否则默认为0。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> tree[100010];
int times[100010];
double res ;

int n;
double power,r;

void dfs(int id,double power)
{
	if(times[id] != 0)
	{
		res += power*times[id];
	}
	else
	{
		for(auto x : tree[id])//自动包含的结束递归的条件
		{
			dfs(x , power * r);
		}
	}
}

int main()
{
	cin>>n>>power>>r;
	r = (100-r)/100; 
	for(int i = 0 ; i < n ; i++)
	{
		int k;cin>>k;
		if(k == 0) cin>>times[i];
	    else
        {
	    	while(k--)
	    	{
	    		int id;cin>>id;
		    	tree[i].push_back(id);
	    	}
		}
	}
	dfs(0,power);
	
	cout<<(long long)res<<endl;
	return 0;
}
posted @ 2026-03-15 16:19  shuiwangrenjia  阅读(1)  评论(0)    收藏  举报