树形 DP 0pts 求调
查看原帖
树形 DP 0pts 求调
1268478
時空楼主2024/12/8 14:07

RT

#include <bits/stdc++.h>

#define FstIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pii pair <ll, ll>
#define pb push_back
#define mem(a, v) memset(a, v, sizeof a)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

const ll N = 2e5 + 7, M = 1e3 + 5;
const ll mod = 1e9 + 7, mod2 = 998244353;
const ld eps = 1e-6;

ll t;

ll L[N], R[N], cost[N];
ll wt[N], p;

ll h[N], nxt[N], to[N], w[N], edge;
ll f[N][M];

ll buildnode(ll Rt, ll v)
{
	wt[Rt] = v;
	return Rt;
}

ll build()
{
	ll Rt = ++ p;
	ll x, y; cin >> x >> y;
	cost[Rt] = x;
	if (!y) L[Rt] = build(), R[Rt] = build();
	else L[Rt] = buildnode((++ p), y);
	return Rt;
}

void add(ll x, ll y, ll c) { nxt[++ edge] = h[x], to[edge] = y, w[edge] = c, h[x] = edge; }

void dfs(ll u)
{
	if (wt[u])
	{
		for (ll i = 0; i <= t; ++ i ) f[u][i] = min(wt[u], i / 5);
		return ;
	}
	for (ll i = h[u]; i; i = nxt[i])
	{
		ll v = to[i], c = w[i];
		dfs(v);
		for (ll j = t; ~j; -- j )
		{
			for (ll k = (c << 1); k <= j; ++ k )
			{
				f[u][j] = max(f[u][j], f[v][k - (c << 1)] + f[u][j - k]); 
			}
		}
	}
}

/*
120
11 0 4 0 8 4 6 0 11 5 16 5 7 0 19 0 10 5 3 1 4 4 

*/

signed main()
{
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	
	FstIO;
	
	cin >> t;
	build();
	for (ll i = 1; i <= p; ++ i ) add(i, L[i], cost[i]), add(i, R[i], cost[i]);
//	for (ll i = 1; i <= p; ++ i ) cout << "i " << L[i] << ' ' << R[i] << ' ' << cost[i] << '\n';
	dfs(1);
	cout << f[1][t - 1] << '\n';
		
	return 0;
	
	cout.flush();
}
2024/12/8 14:07
加载中...