Cod sursa(job #1841433)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 5 ianuarie 2017 17:13:39
Problema Colorare3 Scor 40
Compilator java Status done
Runda Arhiva de probleme Marime 1.17 kb
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	static final int MOD = 1000000000 + 7;
	static ArrayList<ArrayList<Integer> > graph;
	
	static int ans, K;
	static void dfs(int node, int father) {
		int cnt = K;
		if (father > 0)
			cnt --;
		
		for (int i = 0; i < graph.get(node).size(); ++ i)
			if (graph.get(node).get(i) != father) {
				dfs(graph.get(node).get(i), node);
				ans = (int) (((long)cnt * ans) % MOD);
				cnt --;
			}
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		Scanner sc = new Scanner(new FileInputStream("colorare3.in"));
		PrintWriter out = new PrintWriter("colorare3.out");
		
		int N = sc.nextInt();
		K = sc.nextInt();
		
		graph = new ArrayList <ArrayList <Integer> >(N + 1);
		
		for (int i = 0; i <= N; ++ i)
			graph.add(new ArrayList <Integer>());
		
		for (int i = 1; i < N; ++ i) {
			int a = sc.nextInt();
			int b = sc.nextInt();
		
			graph.get(a).add(b);
			graph.get(b).add(a);
		}
		
		ans = 1;
		dfs(1, 0);
		
		out.println(ans);
		out.close();
	}
}