• [NOI 2016]网格


    Description

    题库链接

    给出一张 (n imes m) 的网格,在其中删去 (c) 个格子,问至少再删去几个能使得图上存在两点不连通,或输出无解。

    多组询问,询问组数 (T)

    (1leq Tleq 20,1leq n,mleq 10^9,sum cleq 10^5)

    Solution

    观察题目,容易发现答案只会在 (-1,0,1,2) 之间,那么就可以随机输出其中一个数,以 (frac{1}{4^T}) 的概率通过这道题

    那么分类讨论。

    • 如果答案为 (-1) ,显然只有两种情况:只有剩一个格子,或者只有两个格子且两个格子相邻。这些是可以直接特判的。
    • 如果答案为 (0) ,显然是原图已经出现了不连通的状况。
    • 若果答案为 (1) ,那么原图存在一个点,使得删去这个点后图不联通,即原图存在割点。
    • 否则答案为 (2)

    由于坐标过大,我们不能直接在原图上求解,考虑离散。其实注意到网格上留有的格子有用的只是与每个删掉的格子八连通的格子。

    我们可以把这些格子留下来建图。不过有一种特殊的情况,那就是边界问题,举一个例子

    .....
    ...00
    ...X1
    ...00
    .....

    如题原本是一个 (5 imes 5) 的格子,在 ((3,5)) 被删掉了一个格子,如果我们只考虑八连通,那么就会出现 ( ext{X}) 是一个割点,然而在原图中并不是,所以就考虑将一个被删除的点的周围 (5 imes 5) 的格子(共 (24) 个)都拿下来建新图。这样点数是 (O(24sum c)) 的。

    特判掉 (-1,0) 之后,就跑一遍 ( ext{tarjan}) 判是否有割点即可。

    Code

    我用了 ( ext{map}) ,在 ( ext{UOJ}) 上过不去...( ( ext{bzoj}) 过了就苟且偷生...233333...

    #include <bits/stdc++.h>
    #define pb push_back
    using namespace std;
    const int N = 100000+5;
    const int w1[8] = {1, -1, 0, 0, 1, 1, -1, -1};
    const int w2[8] = {0, 0, 1, -1, -1, 1, -1, 1};
    void gi(int &x) {
        char ch = getchar(); x = 0;
        for (; ch < '0' || ch > '9'; ch = getchar());
        for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x<<1)+(x<<3)+ch-48;
    }
    
    int n, m, c, u, v, idx, color[N<<3], cnt;
    struct pii {
        int first, second;
        pii (int _first = 0, int _second = 0) {first = _first, second = _second; }
        bool operator < (const pii &b) const {
            return first == b.first ? second < b.second : first < b.first;
        }
        bool operator == (const pii &b) const {return first == b.first && second == b.second; }
    };
    pii a[N], t, t1;
    map<pii, int>id, mp;
    queue<pii>Q, P;
    struct tt {int to, next; }edge[N<<6];
    int path[N<<3], top, rk[N<<3], iscut[N<<3], dfn[N<<3], low[N<<3], times;
    
    bool exist(int u, int v) {
        t = a[lower_bound(a+1, a+c+1, pii(u, v))-a];
        return t.first == u && t.second == v;
    }
    bool outof(int x, int y, int u, int d, int l, int r) {
        return x < u || x > d || y < l || y > r;
    }
    void add(int u, int v) {edge[++top] = (tt){v, path[u]}; path[u] = top; }
    bool judge() {
        if (1ll*n*m-c <= 1) return false;
        if (1ll*n*m-c >= 3) return true;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) {
                if (!exist(i, j)) {
                    for (int k = 0; k < 4; k++) {
                        if (outof(i+w1[k], j+w2[k], 1, n, 1, m)) continue;
                        if (!exist(i+w1[k], j+w2[k])) return false;
                    }
                    return true;
                }
            }
    }
    void bfs(pii t) {
        P.push(t); int u, v, u1, v1;
        while (!P.empty()) {
            t = P.front(); P.pop(); u = t.first, v = t.second; int c = color[id[t]];
            for (int k = 0; k < 4; k++)
                if (!outof(u1 = u+w1[k], v1 = v+w2[k], 1, n, 1, m) && !exist(u1, v1) && id.count(t1 = pii(u1, v1))) {
                    if (color[id[t1]]) continue;
                    color[id[t1]] = c; P.push(t1);
                }
        }
    }
    bool connect() {
        while (!Q.empty()) {
            t = Q.front(); Q.pop();
            if (color[id[t]]) continue;
            else color[id[t]] = ++cnt, bfs(t);
        }
        for (int Id = 1; Id <= c; Id++) {
            int u = a[Id].first, v = a[Id].second, c = -1;
            for (int i = -2; i <= 2; i++)
                for (int j = -2; j <= 2; j++) {
                    if (outof(u+i, v+j, 1, n, 1, m) || exist(u+i, v+j)) continue;
                    t = pii(u+i, v+j);
                    if (c == -1) c = color[id[t]];
                    else if (c != color[id[t]]) return false;
                }
        }
        return true;
    }
    void build(int u, int v) {
        for (int i = -2; i <= 2; i++)
            for (int j = -2; j <= 2; j++) {
                if (outof(u+i, v+j, 1, n, 1, m) || exist(u+i, v+j)) continue;
                t = pii(u+i, v+j);
                int x = id.count(t) ? id[t] : (Q.push(t), id[t] = ++idx);
                if (!outof(u+i+1, v+j, max(1, u-2), min(n, u+2), max(1, v-2), min(m, v+2)) && !exist(u+i+1, v+j)) {
                    t = pii(u+i+1, v+j);
                    int y = id.count(t) ? id[t] : (Q.push(t), id[t] = ++idx);
                    if (x > y) {if (!mp.count(pii(y, x))) add(x, y); add(y, x); mp[pii(y, x)] = 1; }
                    else {if (!mp.count(pii(x, y))) add(x, y); add(y, x); mp[pii(x, y)] = 1; }
                }
                if (!outof(u+i, v+j+1, max(1, u-2), min(n, u+2), max(1, v-2), min(m, v+2)) && !exist(u+i, v+j+1)) {
                    t = pii(u+i, v+j+1);
                    int y = id.count(t) ? id[t] : (Q.push(t), id[t] = ++idx);
                    if (x > y) {if (!mp.count(pii(y, x))) add(x, y); add(y, x); mp[pii(y, x)] = 1; }
                    else {if (!mp.count(pii(x, y))) add(x, y); add(y, x); mp[pii(x, y)] = 1; }
                }
                if (abs(i) <= 1 && abs(j) <= 1) rk[x] = 1;
            }
    }
    void tarjan(int u, int fa) {
        iscut[u] = 0; dfn[u] = low[u] = ++times; int sz = 0, v;
        for (int i = path[u]; i; i = edge[i].next) {
            if ((v = edge[i].to) == fa) continue;
            if (!dfn[v]) {
                ++sz; tarjan(v, u);
                if (low[v] >= dfn[u]) iscut[u] = 1;
                low[u] = min(low[u], low[v]);
            }else low[u] = min(low[u], dfn[v]);
        }
        if (fa == 0 && sz == 1) iscut[u] = 0;
    }
    void work() {
        memset(path, top = idx = 0, sizeof(path)); id.clear(), mp.clear();
        memset(dfn, times = 0, sizeof(dfn)); memset(rk, 0, sizeof(rk));
        memset(color, cnt = 0, sizeof(color));
        gi(n), gi(m), gi(c);
        for (int i = 1; i <= c; i++) {
            gi(u), gi(v); a[i] = pii(u, v);
        }
        sort(a+1, a+c+1); a[c+1] = pii(0, 0);
        if (!judge()) {puts("-1"); return; }
        for (int i = 1; i <= c; i++) build(a[i].first, a[i].second);
        if (!connect()) {puts("0"); return; }
        if (n == 1 || m == 1) {puts("1"); return; }
        for (int i = 1; i <= idx; i++) {
            if (!dfn[i]) tarjan(i, 0);
            if (rk[i] && iscut[i]) {puts("1"); return; }
        }
        puts("2");
    }
    int main() {
        int t; gi(t); while (t--) work();
        return 0;
    }
  • 相关阅读:
    C#
    C#
    C#
    创建一个ROS包
    创建一个工作空间
    ROS的文件系统
    单一职责原因
    策略模式
    UML类图
    简单工厂模式
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/9261497.html
Copyright © 2020-2023  润新知