You are a landscape designer tasked with creating a beautiful garden arrangement! You have n gardens labeled from 1 to n, connected by bidirectional paths. Your goal is to plant one of 4 types of flowers (numbered 1, 2, 3, or 4) in each garden.
Here's the challenge: no two gardens connected by a path can have the same type of flower. Think of it as a graph coloring problem where you need to ensure adjacent nodes have different colors!
The good news? Each garden has at most 3 connecting paths, and since you have 4 flower types available, a solution is always guaranteed to exist.
Input: An integer n (number of gardens) and an array paths where paths[i] = [x_i, y_i] represents a bidirectional path between gardens x_i and y_i.
Output: An array answer where answer[i] is the type of flower (1-4) planted in the (i+1)th garden.
Input & Output
Constraints
- 1 โค n โค 104
- 0 โค paths.length โค 2 * 104
- paths[i].length == 2
- 1 โค xi, yi โค n
- Each garden has at most 3 paths coming into or leaving it
- xi โ yi
- It is guaranteed an answer exists