<p>A game on an <strong>undirected</strong> graph is played by two players, Mouse and Cat, who alternate turns.</p>

<p>The graph is given as follows: <code>graph[a]</code> is a list of all nodes <code>b</code> such that <code>ab</code> is an edge of the graph.</p>

<p>The mouse starts at node <code>1</code> and goes first, the cat starts at node <code>2</code> and goes second, and there is a hole at node <code>0</code>.</p>

<p>During each player&#39;s turn, they <strong>must</strong> travel along one&nbsp;edge of the graph that meets where they are.&nbsp; For example, if the Mouse is at node 1, it <strong>must</strong> travel to any node in <code>graph[1]</code>.</p>

<p>Additionally, it is not allowed for the Cat to travel to the Hole (node <code>0</code>).</p>

<p>Then, the game can end in three&nbsp;ways:</p>

<ul>
	<li>If ever the Cat occupies the same node as the Mouse, the Cat wins.</li>
	<li>If ever the Mouse reaches the Hole, the Mouse wins.</li>
	<li>If ever a position is repeated (i.e., the players are in the same position as a previous turn, and&nbsp;it is the same player&#39;s turn to move), the game is a draw.</li>
</ul>

<p>Given a <code>graph</code>, and assuming both players play optimally, return</p>

<ul>
	<li><code>1</code>&nbsp;if the mouse wins the game,</li>
	<li><code>2</code>&nbsp;if the cat wins the game, or</li>
	<li><code>0</code>&nbsp;if the game is a draw.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/cat1.jpg" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> graph = [[2,5],[3],[0,4,5],[1,4,5],[2,3],[0,2,3]]
<strong>Output:</strong> 0
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/17/cat2.jpg" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> graph = [[1,3],[0],[3],[0,2]]
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>3 &lt;= graph.length &lt;= 50</code></li>
	<li><code>1&nbsp;&lt;= graph[i].length &lt; graph.length</code></li>
	<li><code>0 &lt;= graph[i][j] &lt; graph.length</code></li>
	<li><code>graph[i][j] != i</code></li>
	<li><code>graph[i]</code> is unique.</li>
	<li>The mouse and the cat can always move.&nbsp;</li>
</ul>

class Solution {
public:
    int catMouseGame(vector<vector<int>>& graph) {
        int N = graph.size();
        const int DRAW = 0, MOUSE = 1, CAT = 2;

        vector<vector<vector<int>>> color(50, vector<vector<int>>(50, vector<int>(3)));
        vector<vector<vector<int>>> degree(50, vector<vector<int>>(50, vector<int>(3)));

        // Initialize degree[node] : the number of neutral children of this node
        for (int m = 0; m < N; ++m) {
            for (int c = 0; c < N; ++c) {
                degree[m][c][1] = graph[m].size();
                degree[m][c][2] = graph[c].size();
                for (int x : graph[c]) {
                    if (x == 0) {
                        degree[m][c][2]--;
                        break;
                    }
                }
            }
        }

        // Enqueue all nodes that are colored
        queue<vector<int>> q;
        for (int i = 0; i < N; ++i) {
            for (int t = 1; t <= 2; ++t) {
                color[0][i][t] = MOUSE;
                q.push({0, i, t, MOUSE});
                if (i > 0) {
                    color[i][i][t] = CAT;
                    q.push({i, i, t, CAT});
                }
            }
        }

        // Percolate
        while (!q.empty()) {
            vector<int> node = q.front();
            q.pop();
            int i = node[0], j = node[1], t = node[2], c = node[3];

            // Check all possible parents of this node
            for (auto& parent : getParents(graph, i, j, t)) {
                int i2 = parent[0], j2 = parent[1], t2 = parent[2];

                // If this parent is not colored
                if (color[i2][j2][t2] == DRAW) {
                    // If this parent can make a winning move
                    if (t2 == c) {
                        color[i2][j2][t2] = c;
                        q.push({i2, j2, t2, c});
                    } else {
                        // Otherwise, reduce the degree of this parent and enqueue if necessary
                        degree[i2][j2][t2]--;
                        if (degree[i2][j2][t2] == 0) {
                            color[i2][j2][t2] = 3 - t2;
                            q.push({i2, j2, t2, 3 - t2});
                        }
                    }
                }
            }
        }

        return color[1][2][1];
    }

private:
    // Find all possible parents of node (m, c, t)
    vector<vector<int>> getParents(vector<vector<int>>& graph, int m, int c, int t) {
        vector<vector<int>> result;
        if (t == 2) {
            for (int m2 : graph[m])
                result.push_back({m2, c, 3 - t});
        } else {
            for (int c2 : graph[c]) {
                if (c2 > 0)
                    result.push_back({m, c2, 3 - t});
            }
        }
        return result;
    }
};

159725