题解 P3574 【[POI2014]FAR-FarmCraft】

· · 题解

来一发不一样的贪心

qwq调了两天才过,谨以此题解纪念一番

首先观察题面,可知题目给出的是一棵树,再看到(n <= 5e5)的数据范围,明显是不能用暴力算,考虑如何优化

再分析题意:他的汽油恰好够走每条路两遍

什么意思呢?若是要从根节点出发,遍历所有的节点再回到根,每条路径至少要走两遍,此处是 “ 恰好 ” ,也就是说明这样一个意思:当访问一个子节点时,必须将以这个子节点为根的子树走完才能返回

再看题目所求:到达时间 + 安装时间的最大值最小

前文已经得知每条路恰好要走两遍,可知 “ 到达时间 ” 这一条件为定值(n - 1) × 2,那么所求即为遍历完整棵树后剩下还未完成安装的时间。可知以每个子节点为根的子树都能求出这样一个解

这一点就符合每个子节点都是一个子问题这一条件,可以想到树形DP:用f(x)表示以x为根的子树遍历完回到x剩下还未安装完成的时间

如何转换呢?考虑贪心\Rightarrow从每棵子树的根节点出发,一定是先走剩余时间最大的子节点,可以保证情况最优,请读者自行思考

又考虑到在先走这一子节点之后还需走Size(x) - 1(Size(x)指以x为根的子树的子节点个数)个节点,在走这些节点的时候,安装剩余时间是一直在减短的,每条路走两遍,便是2*(Size(x)-1)的时间,再加上从该节点走回根的时间1,共是2*(Size(x)-1)+1=2*Size(x)-1。也可以这么考虑,走完以x为根的子树共需2×Size(x)的时间,除走向该子节点的第一步外,其余都会为其减短时间,故为2×Size(x)-1

以此写出转移方程:f(x)=max(f(x),f(y))(y\in son(x))-(2×Size(x)-1)

然而这只适用于叶子节点的转换,还要考虑到根节点的情况。因根节点在一开始就遍历到了,减短的安装时间就为2×Size(x),故方程:f(x)=max(f(x),c(x)-2*Size(x))

代码就很好写了:

#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分

那么问题出在哪里呢?来看这样一张图:

如若是按上述的计算,答案应为9-2×3+1=4,可实质上最大剩余时间应为7-1=6,由4号节点提供。可知上述推论从大到小遍历的思想并没有错,答案却不一定来源于最大的子节点,因此,还需将根x所有的子节点按从大到小的顺序排序,再进行比较。于是我们就会想到这玩意\downarrow

priority-queue < pair <int, int> > q;

若是开q[5e5]的局部数组会直接炸锅,priority-queue(优先队列)可以自动按从大到小的顺序排列,而pair<,>(二元组)正好可以存下f(y)和y两个量(在这里可能会疑惑为什么要存下变量y呢?请见下文\downarrow)

接下来看转移方程的改动:

当i=1时(当前子节点为y_1,f(y_1)最大时):方程仍为:f(x)=max(f(x),f(y_1)-(2×Size(x)-1))

当i=2时(当前子节点为y_2,f(y_2)第二大时):因以x的最大的子节点为根的子树已经遍历过,故当前节点减短的时间为总共能减短的时间2*Size(x),减去以y_1为根的子树共能减短的时间2*Size(y_1)(这便是上文用pair存下两个变量的原因了,各个x的子节点的编号在这里有用),再减去走向y_1节点来回未能算入的2,及上文提及的走向该节点的一步,整理即为:f(x)=max(f(x),f(y_2)-(2*(Size(x)-Size(y_1)-1)-1))

当i=3时(分别为y_3,f(y3)时):同理需分别减去以y_1,y_2为根的子树,方程为:f(x)=max(f(x),f(y_3)-(2*(Size(x)-Size(y_1)-Size(y_2)-2)-1))

怎么样,是不是发现些规律了?

注意在减去\sum\limits_{j=1}^kSize(y_j)(1<=k<Size(x))后所减的值,当i=1时,所减为0;i=2,所减为1;i=3,所减为2。故我们可以用i-1替换这些数字到方程中去。再设一个变量s,记s=\sum_{y\in son(x)}Size(y),以此来表示在这之前已经遍历过的子节点之和。最终方程如下:

f(x)=max(f(x),f(y)-(2×(Size(x)-s-i)+1))

最后放上完整代码:

#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;
}

如若有什么问题或讲的不清楚的地方,也可以向我提出噢

别喷我码风啊

给管理员大大的说明:前文不是有意要给出错误的解法的,大多说的是思想,错误仅仅是发生在答案的统计上。因下文的正解是在上文的基础上推理的,整篇题解也是本人做此题由错到对的心路历程,读者或许能更好的理解。