<p>You are given an <code>m x n</code> binary grid <code>grid</code> where <code>1</code> represents land and <code>0</code> represents water. An <strong>island</strong> is a maximal <strong>4-directionally</strong> (horizontal or vertical) connected group of <code>1</code>&#39;s.</p>

<p>The grid is said to be <strong>connected</strong> if we have <strong>exactly one island</strong>, otherwise is said <strong>disconnected</strong>.</p>

<p>In one day, we are allowed to change <strong>any </strong>single land cell <code>(1)</code> into a water cell <code>(0)</code>.</p>

<p>Return <em>the minimum number of days to disconnect the grid</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land1.jpg" style="width: 500px; height: 169px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]

<strong>Output:</strong> 2
<strong>Explanation:</strong> We need at least 2 days to get a disconnected grid.
Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land2.jpg" style="width: 404px; height: 85px;" />
<pre>
<strong>Input:</strong> grid = [[1,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Grid of full water is also disconnected ([[1,1]] -&gt; [[0,0]]), 0 islands.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>


class Tarjan {
private:
    const vector<vector<int>>& edges;
    const int n;
    vector<int> dfn, low, fa;
    int timestamp = -1;
public:
    Tarjan(const vector<vector<int>>& edges) : edges(edges), n(edges.size()) {
    }
    void dfs(int u, int parent, vector<int>& res) {
        dfn[u] = low[u] = ++timestamp;
        fa[u] = parent;
        int child = 0;
        bool is_sc = false;
        for (auto& v: edges[u]) {
            if (dfn[v] == -1) {
                dfs(v, u, res);
                child++;
                low[u] = min(low[u], low[v]);
                if (!is_sc && parent != -1 && low[v] >= dfn[u]) {
                    res.push_back(u);
                    is_sc = true;
                }
            } else if (v != parent) {  // 修改：使用 parent 而不是 fa[u]
                low[u] = min(low[u], dfn[v]);  // 修改：使用 dfn[v] 而不是 low[v]
            }
        }
        if (!is_sc && parent == -1 && child >= 2) {
            res.push_back(u);
        }
    }
    int check() {
        low.assign(n, -1);
        dfn.assign(n, -1);
        fa.assign(n, -1);
        timestamp = -1;
        int ccnt = 0;
        vector<int> cvs;
        for (int i = 0; i < n; i++) {
            if (dfn[i] == -1) {
                ccnt++;
                dfs(i, -1, cvs);
            }
        }
        if (ccnt > 1) {
            return 0;
        }
        if (!cvs.empty()) {
            return 1;
        }
        return 2;
    }
};

class Solution {
public:
    int minDays(vector<vector<int>>& grid) {
        const int m = grid.size();
        const int n = grid[0].size();
        int landCount = 0;
        unordered_map<int, int> relabel;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    relabel[i * n + j] = landCount++;
                }
            }
        }
        if (landCount == 0) {
            return 0;
        }
        if (landCount == 1) {
            return 1;
        }
        const int dirs[] = {-1, 0, 1, 0, -1};
        vector<vector<int>> edges(landCount);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    continue;
                }
                for (int k = 0; k < 4; k++) {
                    int ii = i + dirs[k];
                    int jj = j + dirs[k + 1];
                    if (ii >= 0 && ii < m && jj >= 0 && jj < n && grid[ii][jj] == 1) {
                        edges[relabel[i * n + j]].push_back(relabel[ii * n + jj]);
                    }
                }
            }
        }
        auto scc = Tarjan(edges);
        return scc.check();
    }
};

/*
class Tarjan {
private:
    const vector<vector<int>>& edges;
    const int n;
    vector<int> dfn, low, fa;
    int timestamp = -1;

public:
    Tarjan(const vector<vector<int>>& edges) : edges(edges), n(edges.size()) {
    }

    void dfs(int u, int parent, vector<int>& res) {
        dfn[u] = low[u] = ++timestamp;
        fa[u] = parent;
        int child = 0;
        bool is_sc = false;

        for (auto& v: edges[u]) {
            if (dfn[v] == -1) {
                dfs(v, u, res);
                child++;

                low[u] = min(low[u], low[v]);

                if (!is_sc && parent != -1 && low[v] >= dfn[u]) {
                    res.push_back(u);
                    is_sc = true;
                }
            } else if (v != fa[u]) {
                low[u] = min(low[u], low[v]);
            }
        }

        if (!is_sc && parent == -1 && child >= 2) {
            res.push_back(u);
        }
    }

    int check() {
        low.assign(n, -1);
        dfn.assign(n, -1);
        fa.assign(n, -1);
        timestamp = -1;

        int ccnt = 0;
        vector<int> cvs;

        for (int i = 0; i < n; i++) {
            if (dfn[i] == -1) {
                ccnt++;
                dfs(i, -1, cvs);
            }
        }

        if (ccnt > 1) {
            return 0;
        }

        if (!cvs.empty()) {
            return 1;
        }

        return 2;
    }
};

class Solution {
public:
    int minDays(vector<vector<int>>& grid) {
        const int m = grid.size();
        const int n = grid[0].size();

        int landCount = 0;
        unordered_map<int, int> relabel;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    relabel[i * n + j] = landCount++;
                }
            }
        }

        if (landCount == 0) {
            return 0;
        }

        if (landCount == 1) {
            return 1;
        }

        const int dirs[] = {-1, 0, 1, 0, -1};
        vector<vector<int>> edges(landCount);

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    continue;
                }

                for (int k = 0; k < 4; k++) {
                    int ii = i + dirs[k];
                    int jj = j + dirs[k + 1];

                    if (ii >= 0 && ii < m && jj >= 0 && jj < n && grid[ii][jj] == 1) {
                        edges[relabel[i * n + j]].push_back(relabel[ii * n + jj]);
                    }
                }
            }
        }

        auto scc = Tarjan(edges);
        return scc.check();
    }
};
*/

16620