题解 P3574 【[POI2014]FAR-FarmCraft】
来一发不一样的贪心
qwq调了两天才过,谨以此题解纪念一番
首先观察题面,可知题目给出的是一棵树,再看到(
再分析题意:他的汽油恰好够走每条路两遍
什么意思呢?若是要从根节点出发,遍历所有的节点再回到根,每条路径至少要走两遍,此处是 “ 恰好 ” ,也就是说明这样一个意思:当访问一个子节点时,必须将以这个子节点为根的子树走完才能返回
再看题目所求:到达时间 + 安装时间的最大值最小
前文已经得知每条路恰好要走两遍,可知 “ 到达时间 ” 这一条件为定值(
这一点就符合每个子节点都是一个子问题这一条件,可以想到树形
如何转换呢?考虑贪心
又考虑到在先走这一子节点之后还需走
以此写出转移方程:
然而这只适用于叶子节点的转换,还要考虑到根节点的情况。因根节点在一开始就遍历到了,减短的安装时间就为
代码就很好写了:
#include <bits/stdc++.h>
#define re register
#define li inline
li int read() {
re int f, j; re char c;
for (f = 1, c = getchar(); !isdigit(c); f = c == '-' ? -1 : 1, c = getchar());
for (j = 0; isdigit(c); j = (j << 1) + (j << 3) + (c ^ 48), c = getchar());
return f * j;
}
li void write(re int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9) { write(x / 10); } putchar(x % 10 + '0');
}
const int N = 5e5 + 10;
struct Edge {
int next, to;
} e[N << 1];
int n, tot;
int head[N], c[N], Size[N], f[N];
li void add(re int x, re int y) { //链式前向星不多说
e[++tot].to = y; e[tot].next = head[x]; head[x] = tot;
e[++tot].to = x; e[tot].next = head[y]; head[y] = tot;
}
li void update(re int x, re int fa) { //求Size[]
for (re int i = head[x]; i; i = e[i].next) {
re int y = e[i].to;
if (y == fa) { continue; }
update(y, x);
Size[x] += Size[y] + 1;
}
}
li void dfs(re int x, re int fa) {
for (re int i = head[x]; i; i = e[i].next) {
re int y = e[i].to;
if (y == fa) { continue; }
dfs(y, x);
f[x] = std::max(f[x], f[y]);
} if(x != 1) { //特判x = 1的情况(因1是最后安装的
f[x] = std::max(f[x] - 2 * Size[x] + 1, c[x] - 2 * Size[x]);
}
}
signed main() {
n = read();
for (re int i = 1; i <= n; i++) { c[i] = read(); }
for (re int i = 1; i < n; i++) {
re int x = read(), y = read(); add(x, y);
} update(1, 1); dfs(1, 1);
write(std::max(f[1] - 1, c[1]) + (n - 1) * 2); //这里的-1是减去从1的子节点走回1的这步
return 0;
}
可这是错误的,只有30分
那么问题出在哪里呢?来看这样一张图:
如若是按上述的计算,答案应为
若是开
接下来看转移方程的改动:
当
当
当
怎么样,是不是发现些规律了?
注意在减去
最后放上完整代码:
#include <bits/stdc++.h>
#define re register
#define li inline
li int read() {
re int f, j; re char c;
for (f = 1, c = getchar(); !isdigit(c); f = c == '-' ? -1 : 1, c = getchar());
for (j = 0; isdigit(c); j = (j << 1) + (j << 3) + (c ^ 48), c = getchar());
return f * j;
}
li void write(re int x) {
if (x < 0) { putchar('-'); x = -x; }
if (x > 9) { write(x / 10); } putchar(x % 10 + '0');
}
const int N = 5e5 + 10;
struct Edge {
int next, to;
} e[N << 1];
int n, tot;
int head[N], c[N], Size[N], f[N];
li void add(re int x, re int y) { //链式前向星不多说
e[++tot].to = y; e[tot].next = head[x]; head[x] = tot;
e[++tot].to = x; e[tot].next = head[y]; head[y] = tot;
}
li void update(re int x, re int fa) { //求Size[]
for (re int i = head[x]; i; i = e[i].next) {
re int y = e[i].to;
if (y == fa) { continue; }
update(y, x);
Size[x] += Size[y] + 1;
}
}
li bool cmp(std::pair <int, int> x, std::pair <int, int> y) { return x.first > y.first; } //按f(y)从大到小排序
li void dfs(re int x, re int fa) {
std::priority_queue < std::pair <int, int> > q; //定义优先队列
re int tmp, s = 0;
for (re int i = head[x]; i; i = e[i].next) {
re int y = e[i].to;
if (y == fa) { continue; }
dfs(y, x); q.push(std::make_pair(f[y], y)); //插入
} tmp = q.size();
for (re int i = 1; i <= tmp; i++) {
s += Size[q.top().second]; // 累加前子节点之和
f[x] = std::max(f[x], q.top().first - ((Size[x] - s - i) * 2 + 1)); //dp转换
q.pop(); //弹出队首
} f[x] = std::max(f[x], c[x] - 2 * Size[x]); //实际上不特判x = 1也可以(因为在最后输出的时候还有一次max
}
signed main() {
n = read();
for (re int i = 1; i <= n; i++) { c[i] = read(); }
for (re int i = 1; i < n; i++) {
re int x = read(), y = read(); add(x, y);
} update(1, 1); dfs(1, 1);
write(std::max(f[1], c[1]) + (n - 1) * 2);
return 0;
}
如若有什么问题或讲的不清楚的地方,也可以向我提出噢
别喷我码风啊