赛前训练4——总结
wbw_121124
·
·
个人记录
赛前训练4——总结
A - CF1083A
The Fair Nut and the Best Path
错因:未知
诈骗题。虽然可以换根 DP,但是还是 WA 了。
考虑树形 DP:
- 定义 \mathrm{dp}_i 表示以 i 为根结点的子树,从 i 出发的最大剩下的油量。
- 状态转移为:\mathrm{dp}_x = a_x + \max\left(0, S_1\right),其中 S_1 为所有子节点 y 的 \mathrm{dp}_y - w_{xy} 的最大值。
- 最终答案为 a_x + \max\left(0, S_1\right) + \max\left(0, S_2\right),其中 S_2 为次大值。
- 若最大或次大值为负,则其值取 0。
#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。
- 当 $\mathrm{s}_x$ 为 `(` 时:
1. $x$ 进栈;
2. dfs 递归;
3. $x$ 出栈。
- 当 $\mathrm{s}_x$ 为 `)` 时:
1. 当栈为空时,dfs 递归,并跳过下面的步骤;
2. $pre$ 设为栈顶元素;
3. $pre$ 出栈;
4. $\mathrm{dp}_x=\mathrm{dp}_{\mathrm{fa}_pre}$;
5. dfs 递归;
6. $pre$ 进栈。
答案为:
$$
\bigoplus_{i=1}^n i \cdot \left( \sum_{y \in \mathrm{path}(1, i)} \mathrm{dp}_y \right)
$$
其中 $\mathrm{path}(1, i)$ 表示从根节点 $1$ 到节点 $i$ 的路径上的所有节点。
```cpp
#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」一个网的路](https://www.luogu.com.cn/problem/P8595)
定义 $\mathrm{dp}_{i,0/1/2}$ 表示形成一条一头向下的链/炸毁 $i$/形成一条两头向下的链的总操作次数。
```cpp
#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] 可达性](https://www.luogu.com.cn/problem/P12017)
- 定义 $\mathrm{dp}_{i,j}$ 表示到了 $i$,可达结点数为 $j$ 的可行性。
- 状态转移方程为:
$$\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)$$
其中 $x$ 为当前节点,$y$ 为 $x$ 的子节点,$j$ 为可达结点数,$\lor$ 表示“或”操作,$\land$ 表示“与”操作。
- 特殊处理:
- 若 $a_x = a_y$,可直接合并状态;
- 若 $a_x > a_y$,需判断 $dp_{y, a_y}$ 是否可行;
- 若 $a_x < a_y$,需判断 $dp_{y, a_y}$ 或 $dp_{y, a_y - a_x}$ 是否可行。
```cpp
#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;
}
```