Cod sursa(job #1758963)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 18 septembrie 2016 11:40:39
Problema Colorare3 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

const int MAXN = 100000;
const int MOD = 1000000007;

vector<int> g[1 + MAXN];
int answer = 1, k;

void DFS(int node, int father) {
    int subtract = 1;
    if (node == 1)
        subtract = 0;
    for (auto &son : g[node])
        if (son != father) {
            answer = ((long long) answer * (k - subtract)) % MOD;
            subtract++;
            DFS(son, node);
        }
}

int main() {
    int n;
    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);
    }
    DFS(1, 0);
    cout << answer << "\n";
    return 0;
}