Cod sursa(job #2547683)

Utilizator dey44andIoja Andrei-Iosif dey44and Data 15 februarie 2020 16:15:36
Problema Colorare3 Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <bits/stdc++.h>

#define input "colorare3.in"
#define output "colorare3.out"
#define MOD 1000000007
#define NMAX 100005

using namespace std;
typedef long long ll;

ifstream in(input);
ofstream out(output);

vector < int > muchii[NMAX];
queue < int > coada;

ll fact[NMAX], K;
bool uz[NMAX];
int N;

ll fasto_expo(ll base, ll exp)
{
    if(!exp) return 1;
    if(exp == 1) return base % MOD;
    if(exp % 2) return base * fasto_expo((base * base) % MOD, exp / 2) % MOD;
    return fasto_expo((base * base) % MOD, exp / 2) % MOD;
}

ll Arange_Calc(ll n, ll k)
{
    ll A = fact[n];
    ll B = fact[n - k];
    B = fasto_expo(B, MOD - 2) % MOD;
    return (A * B) % MOD;
}

void Read_Data()
{
    in >> N >> K;
    for(int i = 1; i < N; i++)
    {
        int x, y; in >> x >> y;
        muchii[x].push_back(y);
        muchii[y].push_back(x);
    }
    fact[0] = 1;
    for(int i = 1; i <= N; i++)
        fact[i] = (fact[i - 1] * i) % MOD;
}

void Solve()
{
    ll R = 1;
    coada.push(1);
    uz[1] = 1;
    while(!coada.empty())
    {
        int nod = coada.front(); coada.pop();
        ll T = 0;
        for(unsigned i = 0; i < muchii[nod].size(); i++)
        {
            int new_nod = muchii[nod][i];
            if(!uz[new_nod])
            {
                T++; uz[new_nod] = 1; coada.push(new_nod);
            }
        }
        //out << "Suntem in nodul " << nod << " si avem " << T << "\n";
        ll P = 1;
        if(nod == 1)
        {
            for(ll i = K - T + 1; i <= K; i++)
                P = (P * i) % MOD;
        }
        else
        {
            for(ll i = K - T; i < K; i++)
                P = (P * i) % MOD;
        }
        R = (R * P) % MOD;
    }
    if(R < 0) R += MOD;
    out << R << "\n";
}

int main()
{
    Read_Data();
    Solve();
    return 0;
}