Cod sursa(job #1838229)

Utilizator lflorin29Florin Laiu lflorin29 Data 31 decembrie 2016 15:06:26
Problema Colorare3 Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("colorare3.in");
ofstream out("colorare3.out");

const int nmax = 1e5;
int n, k;
vector<int>g[nmax + 1];
const int mod = 1e9 + 7;

int dfs(int node, int father) {
  int sol = 1, nr;

  if (node == 1)
    nr = k;
  else nr = k - 1;

  for (auto i : g[node]) {
    if (i == father)
      continue;

    sol = (1LL * sol * nr) % mod;
    sol = (1LL * sol * dfs(i, node));
    nr--;
  }

  return sol;
}
int main() {
  in >> n >> k;

  for (int i = 1; i < n; ++i) {
    int x, y;
    in >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }

  out << dfs(1, 0);
}