<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 Solution {
public:
    void findArticulationPoints(
        pair<int, int> u, map<pair<int, int>, vector<pair<int, int>>>& adj,
        map<pair<int, int>, bool>& visited, map<pair<int, int>, int>& disc,
        map<pair<int, int>, int>& low,
        map<pair<int, int>, pair<int, int>>& parent,
        map<pair<int, int>, bool>& ap, int& time) {
        int children = 0;
        visited[u] = true;
        disc[u] = low[u] = ++time;

        for (auto v : adj[u]) {
            if (!visited[v]) {
                children++;
                parent[v] = u;
                findArticulationPoints(v, adj, visited, disc, low, parent, ap,
                                       time);
                low[u] = min(low[u], low[v]);

                if (parent[u] == make_pair(-1, -1) && children > 1)
                    ap[u] = true;

                if (parent[u] != make_pair(-1, -1) && low[v] >= disc[u])
                    ap[u] = true;
            } else if (v != parent[u]) {
                low[u] = min(low[u], disc[v]);
            }
        }
    }

    vector<pair<int, int>> findArticulationPointsInGraph(
        map<pair<int, int>, vector<pair<int, int>>>& adj) {
        map<pair<int, int>, bool> visited;
        map<pair<int, int>, int> disc, low;
        map<pair<int, int>, pair<int, int>> parent;
        map<pair<int, int>, bool> ap;
        int time = 0;

        for (auto& node : adj) {
            pair<int, int> u = node.first;
            visited[u] = false;
            disc[u] = -1;
            low[u] = -1;
            parent[u] = make_pair(-1, -1);
            ap[u] = false;
        }

        for (auto& node : adj) {
            pair<int, int> u = node.first;
            if (!visited[u]) {
                findArticulationPoints(u, adj, visited, disc, low, parent, ap,
                                       time);
            }
        }

        vector<pair<int, int>> articulationPoints;
        for (auto& node : ap) {
            if (node.second) {
                articulationPoints.push_back(node.first);
            }
        }
        return articulationPoints;
    }
    map<pair<int, int>, vector<pair<int, int>>>
    cities_in_island(vector<vector<int>>& grid, int m, int n, int x, int y) {
        int cities = 0;
        map<pair<int, int>, vector<pair<int, int>>> adj;
        queue<pair<pair<int, int>, pair<int, int>>> q;
        q.push({{x, y}, {-1, -1}});
        while (!q.empty()) {
            int curr_x = q.front().first.first, curr_y = q.front().first.second;
            int prev_x = q.front().second.first,
                prev_y = q.front().second.second;
            q.pop();
            if (curr_x < 0 || curr_x >= m || curr_y < 0 || curr_y >= n ||
                grid[curr_x][curr_y] == 0)
                continue;
            if (grid[curr_x][curr_y]==-1){
                if (prev_x != -1 && prev_y != -1) {
                    adj[{prev_x, prev_y}].push_back({curr_x, curr_y});
                    adj[{curr_x, curr_y}].push_back({prev_x, prev_y});
                }
                continue;
            }
            grid[curr_x][curr_y] = -1;
            if (prev_x != -1 && prev_y != -1) {
                adj[{prev_x, prev_y}].push_back({curr_x, curr_y});
                adj[{curr_x, curr_y}].push_back({prev_x, prev_y});
            }
            // cities++;
            q.push({{curr_x + 1, curr_y}, {curr_x, curr_y}});
            q.push({{curr_x - 1, curr_y}, {curr_x, curr_y}});
            q.push({{curr_x, curr_y + 1}, {curr_x, curr_y}});
            q.push({{curr_x, curr_y - 1}, {curr_x, curr_y}});
        }
        return adj;
    }
    int minDays(vector<vector<int>>& grid) {
        int ans = 0, m = grid.size(), n = grid[0].size(), rem = 0, islands = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cout << grid[i][j] << " ";
            }
            cout << endl;
        }
        map<pair<int, int>, vector<pair<int, int>>> adj;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    if (islands == 1)
                        return 0;
                    islands++;
                    adj = cities_in_island(grid, m, n, i, j);
                }
            }
        }
        vector<pair<int, int>> articulationPoints =
            findArticulationPointsInGraph(adj);

        for (int i = 0; i < articulationPoints.size(); i++) {
            cout << articulationPoints[i].first << " "
                 << articulationPoints[i].second << endl;
        }
        if(islands==0) return 0;
        if(adj.size()==0) return 1;
        if (articulationPoints.size() >= 1)
            return 1;
        return 2;
    }
};
22780