$我们要< x , y >路径上的点全部同色即根据DFS序来涂即可$
$f[i][j]:前i个点用了j个颜色$
$转移:f[i][j] = f[i-1][j] + f[i-1][j-1] * (k - j + 1);$
图片来自牛客本题讨论
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 310;
vector<int> g[N];
int n, k;
int f[N][N];
int main()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
f[0][0] = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= k; j ++)
{
f[i][j] = (ll)(f[i-1][j] + (ll)f[i-1][j-1] * (k - j + 1) % mod)%mod;
}
ll ans = 0;
for(int i = 1; i <= k; i ++) ans = (ans + f[n][i]) % mod;
cout << ans % mod << endl;
}