跳到正文

个人记录

赛前训练4——总结

wbw_121124约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年10月6日
收录
2025年12月12日
更新
2026年11月19日
篇幅
约 12 分钟

版本

  1. v2当前
  2. v1

赛前训练4——总结

A - CF1083A

The Fair Nut and the Best Path

错因:未知

诈骗题。虽然可以换根 DP,但是还是 WA 了。

考虑树形 DP:

  • 定义 dpi\mathrm{dp}_i 表示以 ii 为根结点的子树,从 ii 出发的最大剩下的油量。
  • 状态转移为:dpx=ax+max⁡(0,S1)\mathrm{dp}_x = a_x + \max\left(0, S_1\right),其中 S1S_1 为所有子节点 yy 的 dpy−wxy\mathrm{dp}_y - w_{xy} 的最大值。
  • 最终答案为 ax+max⁡(0,S1)+max⁡(0,S2)a_x + \max\left(0, S_1\right) + \max\left(0, S_2\right),其中 S2S_2 为次大值。
  • 若最大或次大值为负,则其值取 00。
C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 3e5 + 5;
int n, a[N], dp[N], ans;
vector<pair<int, int>>nbr[N];
void dfs(int x, int fa)
{
	int maxi = 0, maxi1 = 0;
	for (auto& y : nbr[x])
	{
		auto& nxt = y.first, w = y.second;
		if (nxt != fa)
		{
			dfs(nxt, x);
			if (dp[nxt] - w >= maxi)
				maxi1 = maxi, maxi = dp[nxt] - w;
			else
				maxi1 = max(maxi1, dp[nxt] - w);
		}
	}
	dp[x] = maxi + a[x];
	ans = max(ans, maxi + maxi1 + a[x]);
	return;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for (int i = 1; i < n; i++)
	{
		int x, y, z;
		cin >> x >> y >> z;
		nbr[x].push_back({ y,z });
		nbr[y].push_back({ x,z });
	}
	dfs(1, 0);
	cout << ans;
	return 0;
}

B - 洛谷P5658

P5658 [CSP-S2019] 括号树

错因:贪心

用栈维护左括号的位置,进行树形 DP。

dpi\mathrm{dp}_i 表示以 ii 结尾的合法括号串数。

  • 当 sx\mathrm{s}_x 为 ( 时:
    1. xx 进栈;
    2. dfs 递归;
    3. xx 出栈。
  • 当 sx\mathrm{s}_x 为 ) 时:
    1. 当栈为空时,dfs 递归,并跳过下面的步骤;
    2. prepre 设为栈顶元素;
    3. prepre 出栈;
    4. dpx=dpfapre\mathrm{dp}_x=\mathrm{dp}_{\mathrm{fa}_pre};
    5. dfs 递归;
    6. prepre 进栈。

答案为:

⨁i=1ni⋅(∑y∈path(1,i)dpy)\bigoplus_{i=1}^n i \cdot \left( \sum_{y \in \mathrm{path}(1, i)} \mathrm{dp}_y \right)

其中 path(1,i)\mathrm{path}(1, i) 表示从根节点 11 到节点 ii 的路径上的所有节点。

C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 5e5 + 5;
int n, ans, dp[N], fa[N];
string s;
vector<int>nbr[N], st;
void dfs(int x, int fa)
{
	int pre = 0;
	if (s[x] == '(')
		st.push_back(x);
	else if (!st.empty())
	{
		pre = st.back();
		st.pop_back();
		dp[x] = dp[::fa[pre]] + 1;
	}
	for (auto& nxt : nbr[x])
		if (nxt != fa)
			dfs(nxt, x);
	if (s[x] == '(')
		st.pop_back();
	else if (pre)
		st.push_back(pre);
	return;
}
void dfs1(int x, int fa)
{
	ans ^= x * dp[x];
	for (auto& nxt : nbr[x])
		if (nxt != fa)
		{
			dp[nxt] += dp[x];
			dfs1(nxt, x);
		}
	return;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> s;
	s = '#' + s;
	for (int i = 1; i < n; i++)
	{
		int x, y = i + 1;
		cin >> x;
		fa[y] = x;
		nbr[x].push_back(y);
	}
	dfs(1, 0);
	dfs1(1, 0);
	cout << ans;
	return 0;
}

C - 洛谷P8595

P8595 「KDOI-02」一个网的路

定义 dpi,0/1/2\mathrm{dp}_{i,0/1/2} 表示形成一条一头向下的链/炸毁 ii/形成一条两头向下的链的总操作次数。

C++
#include<bits/stdc++.h>
typedef int int32;
//#define int long long
using namespace std;
const int N = 2e6 + 5;
int n, m, ans, dp[N][3];
vector<int>nbr[N];
bool vis[N];
int read()
{
	int x = 0;
	char c = getchar();
	while (c < '0' || c>'9')c = getchar();
	while (c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
	return x;
}
void dfs(int x, int fa)
{
	vis[x] = true;
	dp[x][1] = 2;
	if (nbr[x].size() <= (fa > 0))
		dp[x][0] = 1;
	int maxi = INT_MIN, maxi1 = INT_MIN;
	for (auto& nxt : nbr[x])
		if (nxt != fa)
		{
			dfs(nxt, x);
			dp[x][0] += dp[nxt][1];
			dp[x][1] += min({ dp[nxt][0],dp[nxt][1],dp[nxt][2] });
			if (maxi <= dp[nxt][1] - dp[nxt][2])
			{
				maxi1 = maxi;
				maxi = dp[nxt][1] - dp[nxt][2];
			}
			else
				maxi1 = max(maxi1, dp[nxt][1] - dp[nxt][2]);
		}
	dp[x][0] -= max(0, maxi);
	if (maxi < 0 && nbr[x].size() > (fa > 0))
		dp[x][0]++;
	dp[x][2] = dp[x][0];
	dp[x][0] -= max(0, maxi1 + 1);
	return;
}
signed main()
{
	n = read();
	m = read();
	for (int i = 1; i <= m; i++)
	{
		int x, y;
		x = read();
		y = read();
		nbr[x].push_back(y);
		nbr[y].push_back(x);
	}
	for (int i = 1; i <= n; i++)
		if (!vis[i])
		{
			dfs(i, 0);
			ans += min({ dp[i][0],dp[i][1],dp[i][2] });
		}
	printf("%d", ans - 1);
	return 0;
}

D - 洛谷P12017

P12017 [NOISG 2025 Finals] 可达性

  • 定义 dpi,j\mathrm{dp}_{i,j} 表示到了 ii,可达结点数为 jj 的可行性。

  • 状态转移方程为:

    dpx,j=∨j1+j2=jy∈son(x)(dpx,j1∧dpy,j2)\mathrm{dp}_{x, j} = \lor_{\substack{j_1 + j_2 = j \\ y \in \mathrm{son}(x)}} \left( \mathrm{dp}_{x, j_1} \land \mathrm{dp}_{y, j_2} \right)

    其中 xx 为当前节点,yy 为 xx 的子节点,jj 为可达结点数,∨\lor 表示“或”操作,∧\land 表示“与”操作。

  • 特殊处理:

    • 若 ax=aya_x = a_y,可直接合并状态;
    • 若 ax>aya_x > a_y,需判断 dpy,aydp_{y, a_y} 是否可行;
    • 若 ax<aya_x < a_y,需判断 dpy,aydp_{y, a_y} 或 dpy,ay−axdp_{y, a_y - a_x} 是否可行。
C++
#include<bits/stdc++.h>
typedef int int32;
#define int long long
using namespace std;
const int N = 5e3 + 5;
int n, a[N], size[N];
bitset<2 * N>dp[N], f;
vector<int>nbr[N];
void dfs(int x, int fa)
{
	::size[x] = 1;
	for (auto& nxt : nbr[x])
		if (nxt != fa)
		{
			dfs(nxt, x);
			f.reset();
			if (a[x] == a[nxt])
			{
				for (int i = 0; i <= ::size[x]; i++)
					for (int j = 0; j <= ::size[nxt]; j++)
						f[i + j] = f[i + j] |
						(dp[x][i] && dp[nxt][j]);
				if (dp[nxt][a[nxt]])
					for (int i = 0; i <= ::size[x]; i++)
						f[i] = f[i] | dp[x][i];
			}
			else
				if (a[x] > a[nxt])
					if (!dp[nxt][a[nxt]])
					{
						cout << "NO";
						exit(0);
					}
					else
						for (int i = 0; i <= ::size[x]; i++)
							f[i + a[nxt]] = f[i + a[nxt]] | dp[x][i],
							f[i] = f[i] | dp[x][i];
				else
					if (!dp[nxt][a[nxt]] && !dp[nxt][a[nxt] - a[x]])
					{
						cout << "NO";
						exit(0);
					}
					else
						for (int i = 0; i <= ::size[x]; i++)
							f[i] = f[i] | dp[x][i];
			::size[x] += ::size[nxt];
			dp[x] = f;
		}
	return;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i], dp[i][1] = true;
	for (int i = 1; i < n; i++)
	{
		int x, y;
		cin >> x >> y;
		nbr[x].push_back(y);
		nbr[y].push_back(x);
	}
	dfs(1, 0);
	if (dp[1][a[1]])
		cout << "YES";
	else
		cout << "NO";
	return 0;
}
原文