Cod sursa(job #2253547)

Utilizator AlexandruLuchianov1Alex Luchianov AlexandruLuchianov1 Data 4 octombrie 2018 09:38:57
Problema Colorare3 Scor 50
Compilator cpp Status done
Runda shimulare_shmecheri Marime 1.32 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int const nmax = 100000;
int const modulo = 1000000007;

vector<int> g[5 + nmax];

int fact[5 + nmax];

void gcd(int a ,int b , int &x , int &y){
  if(b == 0){
    x = 1;
    y = 0;
  } else {
    gcd(b , a % b , x , y);
    int aux = x;
    x = y;
    y = aux - a / b * y;
  }
}

void computefact(){
  fact[0] = 1;
  for(int i = 1 ; i <= nmax ; i++)
    fact[i] = 1LL * fact[i - 1] * i % modulo;
}

int dp[5 + nmax];

int form(int x , int y){
  if(y < x)
    return 1;

  x--;

  int a = 0, b = 0;

  gcd(fact[x] , modulo , a , b);
  a %= modulo;
  if(a < 0)
    a += modulo;

  return 1LL * fact[y] * a % modulo;
}

int n , k;



int main()
{
  computefact();

  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);
  }

  for(int i = 1 ; i <= n ; i++){
    if(k < g[i].size()) {
      out << 0;
      return 0;
    }
  }
  int result = 1;

  for(int i = 2 ; i <= n ; i++){
    result = 1LL * result * form(k - g[i].size() + 1 , k - 1) % modulo;
  }
  result = 1LL * result * form(k - g[1].size() + 1, k) % modulo;
  out << result;

  return 0;
}