跳到正文

题解

题解:CF2158E Sink

X_Cross约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年12月24日
收录
2026年1月25日
更新
2026年11月16日
篇幅
约 9 分钟
题目
CF2158E

版本

  1. v2当前
  2. v1
  • 如果把相邻的且权值相同的所有格子看成一个连通块,认为连通块的权值就是格子的权值,然后让权值较小的连通块向权值较大的连通块连边,那么最小挖洞数就是所有入度为 00 的连通块数。

  • 难点在于如何处理连通块以及维护个连通块的入度,考虑用并查集维护连通块,但是可能会涉及把一个格子从连通块中“删去”的操作,一般并查集是做不了的。这时可以用一种 “电诈骗子销号跑路的思维”:给每个格子一个编号,从连通块中删去这个格子操作换成保留并查集中编号,但是把这个格子对入度的贡献减掉,然后把这个格子换一个新的编号。像不像骗子把你的钱骗走后你再说什么都不回应,然后骗子又开个新号去骗其他人。

  • 细节比较多,建议先用边界情况的小数据调的没问题后再提交,代码写的比较繁琐。

C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 4;
int n, m;
int ans;
int siz[MAXN], sum[MAXN], tot;
//siz维护的连通块中的无向边数,sum维护的连通块中每个小格的总入度之和,sum-siz*2就是连通块的入度
int fa[MAXN];
bool vis[MAXN];//开这个数组是为了调试时看见答案统计了哪些连通块
vector<int> a[MAXN];
vector<int> in[MAXN];//维护每小格入度
vector<int> g[MAXN];//维护每小格编号
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
bool check(int x, int y)
{
    return x > 0 && x <= n && y > 0 && y <= m;
}
int _find(int x)
{
    if (fa[x] ==x) return x;
    return fa[x] = _find(fa[x]);
}
void Union(int x, int y)
{
    x = _find(x), y = _find(y);
    if (x == y) return;
    fa[y] = x; sum[x] += sum[y]; siz[x] += siz[y];
}
void update(int x, int y, int c)
{
    int cnt = 0;
    for (int i = 0; i < 4; i++)
    {
        int tx = x + dx[i], ty = y + dy[i];
        if (!check(tx, ty)) continue;
        if (a[x][y] == a[tx][ty] && _find(g[x][y]) == _find(g[tx][ty])) cnt++;
    }
    a[x][y] -= c;
    if (sum[_find(g[x][y])] == (siz[_find(g[x][y])])*2)
    {
        ans--; vis[g[x][y]] = 0;
    }
    int anc = _find(g[x][y]);
    sum[anc] -= in[x][y]; siz[anc] -= cnt;
    g[x][y] = ++tot;
    fa[g[x][y]] = g[x][y]; siz[g[x][y]] = 0; sum[g[x][y]] = 0; in[x][y] = 0;
    for (int i = 0; i < 4; i++)
    {
        int tx = x+dx[i], ty = y + dy[i];
        if (!check(tx, ty)) continue;
        if (a[x][y] <= a[tx][ty] && a[x][y] + c > a[tx][ty])
        {
            in[tx][ty]++; sum[_find(g[tx][ty])]++;
            if (sum[_find(g[tx][ty])] == 1 + (siz[_find(g[tx][ty])])*2)
            {
                ans--; vis[_find(g[tx][ty])] = 0;
            }
        }
        if (a[x][y] >= a[tx][ty])
        {
            in[x][y]++; sum[_find(g[x][y])]++;
        }
    }
    for (int i = 0; i < 4; i++)
    {
        int tx = x+dx[i], ty = y + dy[i];
        if (!check(tx, ty)) continue;
        if (a[x][y] == a[tx][ty])
        {
            Union(g[x][y], g[tx][ty]);
            siz[_find(g[x][y])] ++;
        }
    }
    if (sum[_find(g[x][y])] == (siz[_find(g[x][y])])*2)
    {
        ans++; vis[_find(g[x][y])] = 1;
    }
}
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        tot = 0; ans = 0;
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
            a[i].push_back(0); in[i].push_back(0); g[i].push_back(0);
            for (int j = 1; j <= m; j++)
            {
                int x;
                cin >> x;
                a[i].push_back(x); in[i].push_back(0); g[i].push_back(0);
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                g[i][j] = ++tot; fa[tot] = tot; siz[tot] = 0;
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    int x = i+dx[k], y = j+dy[k];
                    if (!check(x, y)) continue;
                    if (a[i][j] >= a[x][y]) in[i][j]++;
                }
                sum[g[i][j]] += in[i][j];
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    int x = i+dx[k], y = j+dy[k];
                    if (!check(x, y)) continue;
                    if (a[i][j] == a[x][y])
                    {
                        Union(g[i][j], g[x][y]), siz[_find(g[x][y])] ++;
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                int tmp = _find(g[i][j]);
                if (!vis[tmp] && sum[tmp] == (siz[tmp])*2)
                {
                    ans++; vis[tmp] = 1;
                }
            }
        }
        cout << ans <<endl;
        int q;
        cin >> q;
        while(q--)
        {
            int x, y, c;
            cin >> x >> y >> c;
            update(x, y, c);
            cout << ans <<"\n";
        }
        for (int i = 1; i <= tot; i++) vis[i] = sum[i] = siz[i] = fa[i] = 0;
        for (int i = 1; i <= n; i++) in[i].clear(), a[i].clear(), g[i].clear();
    }
    return 0;
}
原文