给定一个带有N个节点和K种颜色的偏斜树(每个节点最多有一个孩子)。您必须为每个节点分配从1到K的颜色,以便父级和子级具有不同的颜色。查找为节点着色的最大方法。
例子 –
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
请注意,只有根和子代(子代,大子代,大子代……等等)应具有不同的颜色。树的根可以选择K种颜色中的任意一种,因此可以选择K种方式。每个其他节点都可以选择除其父节点以外的其他K-1颜色。因此,每个节点都有K-1个选择。
在这里,我们选择树作为每个节点,只有一个孩子。我们可以选择K种颜色中的任意一种作为树的根,因此采用K种方式。我们为它的孩子留下了K-1颜色。因此,对于每个孩子,我们都可以为其父母分配其他颜色。因此,对于N-1个节点中的每一个,我们剩下K-1种颜色。因此,答案是K *(K-1)^(N-1) 。
我们可以通过使用耗费O(N)时间复杂度的普通幂函数来找到答案。但是为了获得更好的时间复杂度,我们使用了快速指数函数,该函数需要O(log N)时间复杂度。
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
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;
}
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.
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
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.
PHP
输出 :
12