PTA-L2-020 Kung Fu Inheritance (25 points)

PTA-L2-020 Kung Fu Inheritance (25 points)

Portal

This is a simple dfs question.
My understanding of this topic is a bit ambiguous. In fact, I don’t know whether the power of martial arts obtained by the Taoist is N times that of the master’s foundation, or the power is doubled after the master has passed the reduced power to himself.
It turns out to be the former.

We use a two-dimensional array to store the relationship. If the next generation of a certain generation is a master, we will mark the master of the master and
mark it with vis[].
Then in the dfs part, if vis[] is not 0, then add the doubling power to ans.
The rest is to recurse all the time, and then pass it on according to the usual power reduction.

Finally, pay attention to the output. The output is not rounded, it is directly discarded, and then remember to convert it to long long type.

code part:

#include <bits/stdc++.h>
#define mst(a, n) memset(a, n, sizeof(a))
using namespace std;
const int N = 1e5 + 10;
const int M = 55;
const int INF = 1e6 + 10;
const double eps = 0.05;
typedef long long ll;

int n;
double z, r;
vector<int> v[N];
int vis[N];
double ans;

void dfs(int x, double now)
{
    
    
	if (vis[x])
	{
    
    
		ans += now * v[x][0];
		return ;
	}
	int n = v[x].size();
	for (int i = 0; i < n; i++)
	{
    
    
		int t = v[x][i];
		dfs(v[x][i], now * 0.01 * (100 - r));
	}
}

int main()
{
    
    
	cin >> n >> z >> r;
	for (int i = 0; i < n; i++)
	{
    
    
		int t;
		int m;
		scanf ("%d", &m);
		if (!m)
		{
    
    
			scanf ("%d", &t);
			vis[i] = 1;
			v[i].push_back(t);
		}
		while (m--)
		{
    
    
			scanf ("%d", &t);
			v[i].push_back(t);
		}
	}
	dfs(0, z);
	cout << (ll)(ans) << endl;
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_44624316/article/details/110236908