Given a tree with **N** nodes. The task is to color the tree with the minimum number of colors(**K**) such that the colors of the edges incident to a vertex are different. Print **K** in first-line and then in next line print **N – 1** space-separated integer represents the colors of the edges.

**Examples:**

Input:N = 3, edges[][] = {{0, 1}, {1, 2}} 0 / 1 / 2Output:2 1 2 0 / (1) 1 / (2) 2Input: N = 8, edges[][] = {{0, 1}, {1, 2}, {1, 3}, {1, 4}, {3, 6}, {4, 5}, {5, 7}} 0 / 1 / \ \ 2 3 4 / \ 6 5 \ 7Output:4 1 2 3 4 1 1 2

**Approach:** First, let’s think about the minimum number of colors **K**. For every vertex **v**, **deg(v) ≤ K** should meet (where **deg(v) **denotes the degree of vertex **v**). In fact, there exists a vertex with all **K** different colors. First, choose a vertex and let it be the root, thus **T** will be a rooted tree. Perform a breadth-first search from the root. For each vertex, determine the colors of edges between its children one by one. For each edge, use the color with the minimum index among those which are not used as colors of edges whose one of endpoints is the current vertex. Then each index of color does not exceed **K**.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
` ` `// Add an edge between the vertexes ` `void` `add_edge(vector<vector<` `int` `> >& gr, ` `int` `x, `
` ` `int` `y, vector<pair<` `int` `, ` `int` `> >& edges) `
`{ ` ` ` `gr[x].push_back(y); `
` ` `gr[y].push_back(x); `
` ` `edges.push_back({ x, y }); `
`} ` ` ` `// Function to color the tree with minimum ` `// number of colors such that the colors of ` `// the edges incident to a vertex are different ` `int` `color_tree(` `int` `n, vector<vector<` `int` `> >& gr, `
` ` `vector<pair<` `int` `, ` `int` `> >& edges) `
`{ ` ` ` ` ` `// To store the minimum colors `
` ` `int` `K = 0; `
` ` ` ` `// To store color of the edges `
` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> color; `
` ` ` ` `// Color of edge between its parent `
` ` `vector<` `int` `> cs(n, 0); `
` ` ` ` `// To check if the vertex is `
` ` `// visited or not `
` ` `vector<` `int` `> used(n, 0); `
` ` ` ` `queue<` `int` `> que; `
` ` `used[0] = 1; `
` ` `que.emplace(0); `
` ` ` ` `while` `(!que.empty()) { `
` ` ` ` `// Take first element of the queue `
` ` `int` `v = que.front(); `
` ` `que.pop(); `
` ` ` ` `// Take the possible value of K `
` ` `if` `(K < (` `int` `)gr[v].size()) `
` ` `K = gr[v].size(); `
` ` ` ` `// Current color `
` ` `int` `cur = 1; `
` ` ` ` `for` `(` `int` `u : gr[v]) { `
` ` ` ` `// If vertex is already visited `
` ` `if` `(used[u]) `
` ` `continue` `; `
` ` ` ` `// If the color is similar `
` ` `// to it's parent `
` ` `if` `(cur == cs[v]) `
` ` `cur++; `
` ` ` ` `// Assign the color `
` ` `cs[u] = color[make_pair(u, v)] `
` ` `= color[make_pair(v, u)] = cur++; `
` ` ` ` `// Mark it visited `
` ` `used[u] = 1; `
` ` ` ` `// Push into the queue `
` ` `que.emplace(u); `
` ` `} `
` ` `} `
` ` ` ` `// Print the minimum required colors `
` ` `cout << K << endl; `
` ` ` ` `// Print the edge colors `
` ` `for` `(` `auto` `p : edges) `
` ` `cout << color[p] << ` `" "` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `n = 8; `
` ` ` ` `vector<vector<` `int` `> > gr(n); `
` ` `vector<pair<` `int` `, ` `int` `> > edges; `
` ` ` ` `// Add edges `
` ` `add_edge(gr, 0, 1, edges); `
` ` `add_edge(gr, 1, 2, edges); `
` ` `add_edge(gr, 1, 3, edges); `
` ` `add_edge(gr, 1, 4, edges); `
` ` `add_edge(gr, 3, 6, edges); `
` ` `add_edge(gr, 4, 5, edges); `
` ` `add_edge(gr, 5, 7, edges); `
` ` ` ` `// Function call `
` ` `color_tree(n, gr, edges); `
` ` ` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` `from` `collections ` `import` `deque as queue `
` ` `gr ` `=` `[[] ` `for` `i ` `in` `range` `(` `100` `)] `
`edges ` `=` `[] `
` ` `# Add an edge between the vertexes ` `def` `add_edge(x, y): `
` ` `gr[x].append(y) `
` ` `gr[y].append(x) `
` ` `edges.append([x, y]) `
` ` `# Function to color the tree with minimum ` `# number of colors such that the colors of ` `# the edges incident to a vertex are different ` `def` `color_tree(n): `
` ` ` ` `# To store the minimum colors `
` ` `K ` `=` `0`
` ` ` ` `# To store color of the edges `
` ` `color ` `=` `dict` `() `
` ` ` ` `# Color of edge between its parent `
` ` `cs ` `=` `[` `0` `] ` `*` `(n) `
` ` ` ` `# To check if the vertex is `
` ` `# visited or not `
` ` `used ` `=` `[` `0` `] ` `*` `(n) `
` ` ` ` `que ` `=` `queue() `
` ` `used[` `0` `] ` `=` `1`
` ` `que.append(` `0` `) `
` ` ` ` `while` `(` `len` `(que) > ` `0` `): `
` ` ` ` `# Take first element of the queue `
` ` `v ` `=` `que.popleft() `
` ` ` ` `# Take the possible value of K `
` ` `if` `(K < ` `len` `(gr[v])): `
` ` `K ` `=` `len` `(gr[v]) `
` ` ` ` `# Current color `
` ` `cur ` `=` `1`
` ` ` ` `for` `u ` `in` `gr[v]: `
` ` ` ` `# If vertex is already visited `
` ` `if` `(used[u]): `
` ` `continue`
` ` ` ` `# If the color is similar `
` ` `# to it's parent `
` ` `if` `(cur ` `=` `=` `cs[v]): `
` ` `cur ` `+` `=` `1`
` ` ` ` `# Assign the color `
` ` `cs[u] ` `=` `cur `
` ` `color[(u, v)] ` `=` `color[(v, u)] ` `=` `cur `
` ` `cur ` `+` `=` `1`
` ` ` ` `# Mark it visited `
` ` `used[u] ` `=` `1`
` ` ` ` `# Push into the queue `
` ` `que.append(u) `
` ` ` ` `# Print minimum required colors `
` ` `print` `(K) `
` ` ` ` `# Print edge colors `
` ` `for` `p ` `in` `edges: `
` ` `i ` `=` `(p[` `0` `], p[` `1` `]) `
` ` `print` `(color[i], end ` `=` `" "` `) `
` ` `# Driver code ` `n ` `=` `8`
` ` `# Add edges ` `add_edge(` `0` `, ` `1` `) `
`add_edge(` `1` `, ` `2` `) `
`add_edge(` `1` `, ` `3` `) `
`add_edge(` `1` `, ` `4` `) `
`add_edge(` `3` `, ` `6` `) `
`add_edge(` `4` `, ` `5` `) `
`add_edge(` `5` `, ` `7` `) `
` ` `# Function call ` `color_tree(n) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

4 1 2 3 4 1 1 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Ways to color a skewed tree such that parent and child have different colors
- Minimum steps to color the tree with given colors
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal
- Sub-tree with minimum color difference in a 2-coloured tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Make a tree with n vertices , d diameter and at most vertex degree k
- Queries to count connected components after removal of a vertex from a Tree
- Largest subtree sum for each vertex of given N-ary Tree
- Assign weights to edges such that longest path in terms of weights is minimized
- Number of distinct pair of edges such that it partitions both trees into same subsets of nodes
- Possible edges of a tree for given diameter, height and vertices
- Number of edges in mirror image of Complete binary tree
- Number of edges in a perfect binary tree with N levels
- Right sibling of each node in a tree given as array of edges
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Minimum number of edges between two vertices of a Graph
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Count of distinct colors in a subtree of a Colored Tree with given min frequency for Q queries

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.