Pagini recente » Cod sursa (job #307900) | Cod sursa (job #664772) | Cod sursa (job #2137515) | Cod sursa (job #823912) | Cod sursa (job #1740967)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("colorare3.in");
ofstream fout("colorare3.out");
const int NMAX = 1e5 + 5;
const int MOD = 1e9 + 7;
int n; int k;
vector<int> g[NMAX];
int dfs(int node, int father) {
int ans = 1;
int root = (node == 1) ? 0 : 1;
int sons = 0;
for(int i = 0 ; i < g[node].size(); ++i)
if(g[node][i] != father) {
ans = (1ll * ans * (k - root - sons) * dfs(g[node][i], node)) % MOD;
sons++;
}
return ans;
}
int main() {
fin >> n >> k;
for(int i = 1; i < n; ++i) {
int x; int y; fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i = 1 ; i <= n ; ++i)
if(g[i].size() > k) {
fout << 0 << '\n';
exit(0);
}
fout << dfs(1, 0) << '\n';
return 0;
}