<p>You have <code>n</code> gardens, labeled from <code>1</code> to <code>n</code>, and an array <code>paths</code> where <code>paths[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> describes a bidirectional path between garden <code>x<sub>i</sub></code> to garden <code>y<sub>i</sub></code>. In each garden, you want to plant one of 4 types of flowers.</p>

<p>All gardens have <strong>at most 3</strong> paths coming into or leaving it.</p>

<p>Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.</p>

<p>Return <em><strong>any</strong> such a choice as an array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> is the type of flower planted in the </em><code>(i+1)<sup>th</sup></code><em> garden. The flower types are denoted </em><code>1</code><em>, </em><code>2</code><em>, </em><code>3</code><em>, or </em><code>4</code><em>. It is guaranteed an answer exists.</em></p>

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

<pre>
<strong>Input:</strong> n = 3, paths = [[1,2],[2,3],[3,1]]
<strong>Output:</strong> [1,2,3]
<strong>Explanation:</strong>
Gardens 1 and 2 have different types.
Gardens 2 and 3 have different types.
Gardens 3 and 1 have different types.
Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].
</pre>

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

<pre>
<strong>Input:</strong> n = 4, paths = [[1,2],[3,4]]
<strong>Output:</strong> [1,2,1,2]
</pre>

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

<pre>
<strong>Input:</strong> n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
<strong>Output:</strong> [1,2,3,4]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= paths.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>paths[i].length == 2</code></li>
	<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n</code></li>
	<li><code>x<sub>i</sub> != y<sub>i</sub></code></li>
	<li>Every garden has <strong>at most 3</strong> paths coming into or leaving it.</li>
</ul>


class Solution {  
private:
     void bfs(int start, vector<vector<int>>& graph, vector<int>& result) {
        queue<int> q;
        q.push(start);
        result[start] = 1; // Assign the first type of flower initially (color = 1)

        while (!q.empty()) {
            int garden = q.front();
            q.pop();
            bool used[5] = {}; // Track used flowers, index 0 is unused

            // Mark the flowers used by adjacent gardens
            for (int adj : graph[garden]) {
                if (result[adj] != 0) {
                    used[result[adj]] = true;
                }
            }

            // Assign the smallest available flower type to the current garden
            for (int flower = 1; flower <= 4; flower++) {
                if (!used[flower]) {
                    result[garden] = flower;
                    break;
                }
            }

            // Add unvisited adjacent gardens to the queue
          for (int adj : graph[garden]) {
    // If the adjacent garden has not been assigned a flower yet
    if (result[adj] == 0) {
        q.push(adj); // Add the adjacent garden to the BFS queue
        result[adj] = 1; // Temporarily assign the first type of flower
        // This temporary assignment prevents the garden from being re-added to the queue
        // and ensures that when it is processed, it will be reconsidered for the correct flower type
    }
}
        }
    }
    
public:
    vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {
        vector<vector<int>> graph(n);
        for (auto& path : paths){
            // Convert 1-based index to 0-based
            int u = path[0] - 1;
            int v = path[1] - 1;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        vector<int> result(n, 0); // Result array, initialized to 0 (no flower)
        for (int i = 0; i < n;i++){
            if (result[i] == 0){ // If garden i has no flower assigned yet
                bfs(i, graph, result);
            }
        }
        return result;
    }
};
64845