Cod sursa(job #1740965)

Utilizator TeodorCotetCotet Teodor TeodorCotet Data 12 august 2016 17:16:13
Problema Colorare3 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("color3.in");
ofstream fout("color3.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);
	}

	fout << dfs(1, 0) << '\n';

	return 0;
}