<p>Given a 2D array of characters <code>grid</code> of size <code>m x n</code>, you need to find if there exists any cycle consisting of the <strong>same value</strong> in <code>grid</code>.</p>

<p>A cycle is a path of <strong>length 4 or more</strong> in the grid that starts and ends at the same cell. From a given cell, you can move to one of the cells adjacent to it - in one of the four directions (up, down, left, or right), if it has the <strong>same value</strong> of the current cell.</p>

<p>Also, you cannot move to the cell that you visited in your last move. For example, the cycle <code>(1, 1) -&gt; (1, 2) -&gt; (1, 1)</code> is invalid because from <code>(1, 2)</code> we visited <code>(1, 1)</code> which was the last visited cell.</p>

<p>Return <code>true</code> if any cycle of the same value exists in <code>grid</code>, otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/1.png" style="width: 231px; height: 152px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;,&quot;a&quot;],[&quot;a&quot;,&quot;a&quot;,&quot;a&quot;,&quot;a&quot;]]
<strong>Output:</strong> true
<strong>Explanation: </strong>There are two valid cycles shown in different colors in the image below:
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/11.png" style="width: 225px; height: 163px;" />
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/22.png" style="width: 236px; height: 154px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;c&quot;,&quot;c&quot;,&quot;c&quot;,&quot;a&quot;],[&quot;c&quot;,&quot;d&quot;,&quot;c&quot;,&quot;c&quot;],[&quot;c&quot;,&quot;c&quot;,&quot;e&quot;,&quot;c&quot;],[&quot;f&quot;,&quot;c&quot;,&quot;c&quot;,&quot;c&quot;]]
<strong>Output:</strong> true
<strong>Explanation: </strong>There is only one valid cycle highlighted in the image below:
<img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/2.png" style="width: 229px; height: 157px;" />
</pre>

<p><strong class="example">Example 3:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/07/15/3.png" style="width: 183px; height: 120px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;a&quot;,&quot;b&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;z&quot;,&quot;b&quot;],[&quot;b&quot;,&quot;b&quot;,&quot;a&quot;]]
<strong>Output:</strong> false
</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;= 500</code></li>
	<li><code>grid</code> consists only of lowercase English letters.</li>
</ul>

#include <vector>

using namespace std;

int moveX[] = { -1, 1, 0, 0 };
int moveY[] = { 0, 0, -1, 1 };

struct UnionFind {
public:
    UnionFind(int n) {
        parents = vector<int>(n);
        ranks = vector<int>(n, 1);

        for (int i = 0; i < n; ++i) {
            parents[i] = i;
        }
    }

    int find(int u) {
        if (parents[u] != u) {
            parents[u] = find(parents[u]);
        }

        return parents[u];
    }

    bool unionSet(int u, int v) {
        int rootU = find(u);
        int rootV = find(v);

        if (rootU == rootV) {
            return false;
        }

        if (ranks[rootU] > ranks[rootV]) {
            parents[rootV] = rootU;
        } else if (ranks[rootU] < ranks[rootV]) {
            parents[rootU] = rootV;
        } else {
            parents[rootV] = rootU;
            ranks[rootU]++;
        }

        return true;
    }

public:
    vector<int> parents;
    vector<int> ranks;
};

class Solution {
public:
    int N;
    int M;

    using Visited = vector<vector<bool>>;
    Visited visited;

    bool dfs(int x, int y, int parentX, int parentY, const vector<vector<char>>& grid, UnionFind &unionFind) {
        visited[x][y] = true;
        int u = x * M + y;

        for (int i = 0; i < 4; ++i) {
            int xx = x + moveX[i];
            int yy = y + moveY[i];

            if (xx < 0 || xx >= N || yy < 0 || yy >= M) {
                continue;
            }

            if (grid[x][y] != grid[xx][yy]) {
                continue;
            }

            int v = xx * M + yy;

            if (!visited[xx][yy]) {
                if (!unionFind.unionSet(u, v)) {
                    return true;
                }
                if (dfs(xx, yy, x, y, grid, unionFind)) {
                    return true;
                }
            } else if (xx != parentX || yy != parentY) {
                return true;  // Phát hiện chu trình
            }
        }

        return false;
    }

    bool containsCycle(vector<vector<char>>& grid) {
        N = grid.size();
        M = grid[0].size();

        visited = Visited(N, vector<bool>(M, false));
        UnionFind unionFind(N * M);

        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                if (!visited[i][j]) {
                    if (dfs(i, j, -1, -1, grid, unionFind)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
};

107889