# Ways to color a skewed tree such that parent and child have different colors

Given a skewed tree (Every node has at most one child) with N nodes and K colors. You have to assign a color from 1 to K to each node such that parent and child has different colors. Find the **maximum** number of **ways** of coloring the nodes.

**Examples –**

Input : N = 2, K = 2. Output : Let A1 and A2 be the two nodes. Let A1 is parent of A2. Colors are Red and Blue. Case 1: A1 is colored Red and A2 is colored Blue. Case 2: A1 is colored Blue and A2 is colored Red. No. of ways : 2 Input : N = 3, K = 3. Output : A1, A2, A3 are the nodes. A1 is parent of A2 and A2 is parent of A3. Let colors be R, B, G. A1 can choose any three colors and A2 can choose any other two colors and A3 can choose any other two colors than its parents. No. of ways: 12

Note that **only the root** and **children** (children, grand children, grand grand children …. and all) should have **different** colors. The root of the tree can choose any of the K colors so K ways. Every other node can choose other K-1 colors other than its parent. So every node has K-1 choices.

Here, we select the tree as every node as only one child. We can choose any of the K colors for the root of the tree so K ways. And we are left with K-1 colors for its child. So for every child we can assign a color other than its parent. Thus, for each of the N-1 nodes we are left with K-1 colors. Thus the answer is **K*(K-1)^(N-1)**.

We can find the answer by using normal power function which takes O(N) time complexity. But for better time complexity we use Faster Exponentiation function which takes O(log N) time complexity.

## C++

`// C++ program to count number of ways to color ` `// a N node skewed tree with k colors such that ` `// parent and children have different colors. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// fast_way is recursive ` `// method to calculate power ` `int` `fastPow(` `int` `N, ` `int` `K) ` `{ ` ` ` `if` `(K == 0) ` ` ` `return` `1; ` ` ` `int` `temp = fastPow(N, K / 2); ` ` ` `if` `(K % 2 == 0) ` ` ` `return` `temp * temp; ` ` ` `else` ` ` `return` `N * temp * temp; ` `} ` ` ` `int` `countWays(` `int` `N, ` `int` `K) ` `{ ` ` ` `return` `K * fastPow(K - 1, N - 1); ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `N = 3, K = 3; ` ` ` `cout << countWays(N, K); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number of ways to color ` `// a N node skewed tree with k colors such that ` `// parent and children have different colors. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// fast_way is recursive ` ` ` `// method to calculate power ` ` ` `static` `int` `fastPow(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `if` `(K == ` `0` `) ` ` ` `return` `1` `; ` ` ` `int` `temp = fastPow(N, K / ` `2` `); ` ` ` `if` `(K % ` `2` `== ` `0` `) ` ` ` `return` `temp * temp; ` ` ` `else` ` ` `return` `N * temp * temp; ` ` ` `} ` ` ` ` ` `static` `int` `countWays(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `return` `K * fastPow(K - ` `1` `, N - ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `3` `, K = ` `3` `; ` ` ` `System.out.println(countWays(N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count ` `# number of ways to color ` `# a N node skewed tree with ` `# k colors such that parent ` `# and children have different ` `# colors. ` ` ` `# fast_way is recursive ` `# method to calculate power ` `def` `fastPow(N, K): ` ` ` `if` `(K ` `=` `=` `0` `): ` ` ` `return` `1` `; ` ` ` ` ` `temp ` `=` `fastPow(N, ` `int` `(K ` `/` `2` `)); ` ` ` `if` `(K ` `%` `2` `=` `=` `0` `): ` ` ` `return` `temp ` `*` `temp; ` ` ` `else` `: ` ` ` `return` `N ` `*` `temp ` `*` `temp; ` ` ` `def` `countWays(N, K): ` ` ` `return` `K ` `*` `fastPow(K ` `-` `1` `, N ` `-` `1` `); ` ` ` `# Driver Code ` `N ` `=` `3` `; ` `K ` `=` `3` `; ` `print` `(countWays(N, K)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count number of ways ` `// to color a N node skewed tree with ` `// k colors such that parent and ` `// children have different colors ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// fast_way is recursive ` ` ` `// method to calculate power ` ` ` `static` `int` `fastPow(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `if` `(K == 0) ` ` ` `return` `1; ` ` ` `int` `temp = fastPow(N, K / 2); ` ` ` `if` `(K % 2 == 0) ` ` ` `return` `temp * temp; ` ` ` `else` ` ` `return` `N * temp * temp; ` ` ` `} ` ` ` ` ` `static` `int` `countWays(` `int` `N, ` `int` `K) ` ` ` `{ ` ` ` `return` `K * fastPow(K - 1, N - 1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 3, K = 3; ` ` ` `Console.WriteLine(countWays(N, K)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count number ` `// of ways to color a N node ` `// skewed tree with k colors ` `// such that parent and children ` `// have different colors. ` ` ` `// fast_way is recursive ` `// method to calculate power ` `function` `fastPow(` `$N` `, ` `$K` `) ` `{ ` ` ` `if` `(` `$K` `== 0) ` ` ` `return` `1; ` ` ` ` ` `$temp` `= fastPow(` `$N` `, ` `$K` `/ 2); ` ` ` ` ` `if` `(` `$K` `% 2 == 0) ` ` ` `return` `$temp` `* ` `$temp` `; ` ` ` `else` ` ` `return` `$N` `* ` `$temp` `* ` `$temp` `; ` `} ` ` ` `function` `countWays(` `$N` `, ` `$K` `) ` `{ ` ` ` `return` `$K` `* fastPow(` `$K` `- 1, ` `$N` `- 1); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$N` `= 3; ` ` ` `$K` `= 3; ` ` ` `echo` `countWays(` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

12

This article is contributed by **Harsha Mogali**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Color tree with minimum colors such that colors of edges incident to a vertex are different
- Sum of all parent-child differences in a Binary Tree
- Sum of all the child nodes with even parent values in a Binary Tree
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Minimum steps to color the tree with given colors
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Sum of all the parent nodes having child node x
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Skewed Binary Tree
- Color N boxes using M colors such that K boxes have different color from the box on its left
- Check whether a given binary tree is skewed binary tree or not?
- Height of n-ary tree if parent array is given
- Height of a generic tree from parent array
- Maximum parent children sum in Binary tree
- Find the parent of a node in the given binary tree
- Find parent of each node in a tree for multiple queries
- Find right sibling of a binary tree with parent pointers
- Construct Binary Tree from given Parent Array representation
- Print the nodes having exactly one child in a Binary tree
- Sum of all the child nodes with even grandparents in a Binary Tree