K. King's Colors
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Far, far away, there is the mystical Kingdom of Trees (more formally, "Royal Commonwealth of Connected Undirected Simple Acyclic Graphs"). The King there is very sad because his kingdom is not accepted as a sovereign state in the United Nations. In order to become a member, he needs to make a flag the UN can put on their website.
The flag will of course consist of the King's favorite tree, which contains nn nodes. The King would be happy to just have the tree colored in black and white, but for the sake of his wife the Queen, he decided that the tree will contain all the favorite colors of their kk children (they all have distinct favorite colors). Clearly, no two neighboring nodes can have the same color, but otherwise any coloring of the tree using exactly the kk colors would make a feasible flag candidate.
How many different flags are possible?
Input
The first line contains two integers nn and kk (2 ≤ k ≤ n ≤ 2500 2 ≤ k ≤ n ≤ 2500), where nn is the number of nodes in the King's favorite tree and kk is the number of children. Then follow n−1 lines describing the edges in the tree; the ii'th of these lines contains a non-negative integer pi<ipi<i, meaning that node pipi is the parent of ii.
The nodes are numbered from 0 to n−1 and the tree is rooted at node 0. Note that the embedding of the tree on the flag is already fixed, the only thing that remains is to assign colors.
Output
Output the number of different possible color assignments. The number can be quite big, so the King has requested to know the answer modulo 10000000071000000007.
Examples
input
Copy
4 3 0 1 1output
Copy
18input
Copy
6 4 0 1 1 3 4output
Copy
600
【思路】
题意是国王有一棵树,他的孩子们喜欢K种颜色,希望国王能把这K种颜色涂到树的N个节点上,而且要保证相邻节点不同色,问涂色方案数。
我们令f(i)表示把至多i种颜色涂到树上的方案数,g(i)表示正好i种颜色涂到树上的方案数,显然这就有以下两项事实:
我们得到f(i)是很容易的,要求g(k)却很难,那就需要反推g(i)的求法。
然后照搬二项式反演:。
【代码】
//******************************************************************************
// File Name: K.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: Sat 27 Oct 2018 06:36:50 PM CST
//******************************************************************************
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2505, MOD = 1e9 + 7;
int n, k;
ll C[MAXN][MAXN];
void init()
{
C[0][0] = 1; C[0][1] = 0;
for (int i = 1; i <= 2500; i++) {
C[i][0] = 1; C[i][i + 1] = 0;
for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
}
}
ll qpow(ll a, ll b, ll c)
{
ll ans = 1;
while (b) {
if (b & 1) ans = ans * a % c;
a = a * a % c;
b >>= 1;
}
return ans;
}
int main()
{
scanf("%d %d", &n, &k);
for (int i = 1; i < n; i++) {
int x;
scanf("%d", &x);
}
init();
ll ans = 0;
for (int i = k, j = 1; i >= 0; i--, j = -j)
ans = ((ans + (ll)j * C[k][i] * i % MOD * qpow(i - 1, n - 1, MOD) % MOD) % MOD + MOD) % MOD;
printf("%lld\n", ans);
return 0;
}