题解:AT_abc413_g [ABC413G] Big Banned Grid

· · 题解

一道完全不配被放在 G 的题目,甚至没有 F 难。

由题知 (1,1) 走不到 (H,K) 的充要条件是右上边界和左下边界被八连通的障碍块连通,并查集检验即可。有人说这个是最大流最小割定理的什么的,我不知道,总之不影响做这道题。

时间复杂度 O(K\alpha(K)),我的具体实现中会带个 \log,因为我用了 map,实际上可以拆成 unordered_map 来消掉。

// #define Redshift_Debug
#ifdef Redshift_Debug
#define debug(...) fprintf(stderr, __VA_ARGS__)
#include <chrono>
#else
#define debug(...)
#endif
#include <cstdio>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
const int N = 2e5 + 10;
int n, m, k, x[N], y[N], f[N];
using pii = pair<int, int>;
map<pii, int> mp;
inline int find(int x)
{
    return x == f[x] ? x : f[x] = find(f[x]);
}
inline void merge(int x, int y)
{
    x = find(x), y = find(y);
    if (x == y)
        return;
    f[y] = x;
}
const int dx[]{0, 1, 1, 1, 0, -1, -1, -1}, dy[]{1, 1, 0, -1, -1, -1, 0, 1};
void run()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    f[k + 1] = k + 1;
    for (int i = 1; i <= k; i++)
    {
        f[i] = i;
        cin >> x[i] >> y[i];
        mp[pii{x[i], y[i]}] = i;
        if (x[i] == 1 or y[i] == m)
            merge(0, i);
        if (x[i] == n or y[i] == 1)
            merge(k + 1, i);
    }
    for (int i = 1; i <= k; i++)
    {
        for (int j = 0, nx, ny; j < 8; j++)
        {
            nx = x[i] + dx[j], ny = y[i] + dy[j];
            if (!mp.count({nx, ny}))
                continue;
            merge(i, mp[pii{nx, ny}]);
        }
    }
    if (find(0) == find(k + 1))
        cout << "No\n";
    else
        cout << "Yes\n";
}
int main()
{
#ifdef Redshift_Debug
    auto st = chrono::high_resolution_clock::now();
#endif
    run();
#ifdef Redshift_Debug
    auto ed = chrono::high_resolution_clock::now();
    fprintf(stderr, "%.9lf\n", (ed - st).count() / 1e9);
#endif
}