签到题题解

· · 个人记录

拿到这道题,我相信 O(n^2m^2 \log n^2m^2) 的做法应该很容易想到吧!直接暴力连接两个为 1 的点,然后跑遍最小生成树就可以了。

接下来想想怎么优化,这道题的复杂度瓶颈显然是边数,我们发现有很多边是多余的,那我们想想怎么优化边的数量。

考虑下面这种情况:

我们发现连接 C 与 A,B 任意一条边长度一定不如连接 A,B 优,于是我们考虑把所有类似 C 的边删掉。具体地,对于 a,b,c 三点,坐标分别为 (x1,y1),(x2,y2),(x3,y3),且满足 x1 \leq x2 \leq x3,y1 \leq y2 \leq y3,则一定有 dis((x1,y1),(x3,y3))+dis((x1,y1),(x2,y2)) \textup{\textbf{and}} dis((x1,y1),(x3,y3))+dis((x2,y2),(x3,y3)) \geq dis((x1,y1),(x2,y2))+dis((x2,y2),(x3,y3)),因此我们需要连接 x,y 当且仅当在两点形成的矩形内没有任何其他的点。

得出这个结论后我们发现每一排至多连 m 条边出去(显然),然后每个点还可以想左右连边(同排),所以总变数变为了 2nm。

此时再跑一遍最小生成树就过了。

似乎还没讲怎么建图。为找出这些边,我们固定一个扫描方向。以“向右”的方向为例,按列从左到右扫描网格,并在每一列中从下往上处理。当处理到某个点时,已经被扫描到的点必然位于它的右侧,因此只需在这些点中寻找可能与它形成最优连边的点。

在扫描过程中,用一个单调栈维护候选点集合。栈中保存的是在当前方向上仍然“可见”的点。栈的单调性是:若新扫描到的点在几何位置上比栈顶点更优,则说明栈顶点已经被这个新点遮挡,不可能成为任何点的连边对象,应当从栈中弹出。这个过程持续进行,直到栈满足单调性。

当扫描到一个真正的点时,此时栈中剩下的所有点,正是该点在这个方向上所有“未被遮挡”的候选点。这些点中,任意一个都有可能与当前点形成最小生成树中的边,因此将当前点与栈中所有点建立连边即可。

这道题我们只需要从右至左和从左至右各跑一遍即可建完图。

建完图后跑 Kruskal 就可以 AC 此题。

Code

#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
using namespace std;
int n, m, sum, cnt, ks, ans;
PII ys[4000010];
int a[2010][2010], L[2010][2010], R[2010][2010], fa[4000010];
stack<PII> st;
struct QUE {
    int x, y, l;
    bool operator < (const QUE &xx) const { return l < xx.l; }
};
QUE edge[8000010];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(int i = 1;i <= n;i ++) {
        char c;
        for(int j = 1;j <= m;j ++) cin >> c, a[i][j] = (c == '1' ? ++ sum : 0), ys[a[i][j]] = {i, j}, fa[sum] = sum;
    }
    if(sum <= 1) {
        cout << 0;
        return 0;
    }
    for(int i = 1;i <= n;i ++) {
        for(int j = 1;j <= m;j ++) L[i][j] = (a[i][j - 1] > 0 ? j - 1 : L[i][j - 1]);
        for(int j = m;j >= 1;j --) R[i][j] = (a[i][j + 1] > 0 ? j + 1 : R[i][j + 1]);
    }
    for(int j = 1;j <= m;j ++) {
        for(int i = n;i >= 1;i --) {
            if(R[i][j]) {
                while(!st.empty() && st.top().second >= R[i][j]) st.pop();
                st.push({i, R[i][j]});
            }
            if(a[i][j]) {
                for(;!st.empty();st.pop()) edge[++ cnt] = {a[i][j], a[st.top().first][st.top().second], (i - st.top().first) * (i - st.top().first) + (j - st.top().second) * (j - st.top().second)};
                st.push({i, j});
            }
        }
        while(!st.empty()) st.pop();
    }
    for(int j = m;j >= 1;j --) {
        for(int i = n;i >= 1;i --) {
            if(L[i][j]) {
                while(!st.empty() && st.top().second <= L[i][j]) st.pop();
                st.push({i, L[i][j]});
            }
            if(a[i][j]) {
                for(;!st.empty();st.pop()) if(st.top().first != i && st.top().second != j) edge[++ cnt] = {a[i][j], a[st.top().first][st.top().second], (i - st.top().first) * (i - st.top().first) + (j - st.top().second) * (j - st.top().second)};
                st.push({i, j});
            }
        }
        while(!st.empty()) st.pop();
    }
    sort(edge + 1, edge + cnt + 1);
    for(int i = 1;i <= cnt && sum > 1;i ++) {
        int fu = find(edge[i].x), fv = find(edge[i].y);
        if(fu == fv) continue;
        fa[fv] = fu, sum --;
        if(edge[i].l > 2) ans += edge[i].l;
    }
    cout << ans;
    return 0;
}

都做出来了这道题,说明你有实力写出这道题的原题 草地 的预处理,那快去写原题吧!