赛前训练4——总结
A - CF1083A
The Fair Nut and the Best Path
错因:未知
诈骗题。虽然可以换根 DP,但是还是 WA 了。
考虑树形 DP:
- 定义 表示以 为根结点的子树,从 出发的最大剩下的油量。
- 状态转移为:,其中 为所有子节点 的 的最大值。
- 最终答案为 ,其中 为次大值。
- 若最大或次大值为负,则其值取 。
#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
错因:贪心
用栈维护左括号的位置,进行树形 DP。
表示以 结尾的合法括号串数。
- 当 为
(时:- 进栈;
- dfs 递归;
- 出栈。
- 当 为
)时:- 当栈为空时,dfs 递归,并跳过下面的步骤;
- 设为栈顶元素;
- 出栈;
- ;
- dfs 递归;
- 进栈。
答案为:
其中 表示从根节点 到节点 的路径上的所有节点。
#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
定义 表示形成一条一头向下的链/炸毁 /形成一条两头向下的链的总操作次数。
#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] 可达性
-
定义 表示到了 ,可达结点数为 的可行性。
-
状态转移方程为:
其中 为当前节点, 为 的子节点, 为可达结点数, 表示“或”操作, 表示“与”操作。
-
特殊处理:
- 若 ,可直接合并状态;
- 若 ,需判断 是否可行;
- 若 ,需判断 或 是否可行。
#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;
}