Ways to color a skewed tree such that parent and child have different colors | 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 . ; fast_way is recursive method to calculate power ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ) ; } int main ( ) { int N = 3 , K = 3 ; cout << countWays ( N , K ) ; return 0 ; }