#P4858. Valid Sets
Valid Sets
Valid Sets
题面翻译
给定 个点的树,点有点权,求满足最大点权与最小点权之差小于等于 的连通子图数目。答案对 取模。
题目描述
As you know, an undirected connected graph with nodes and edges is called a tree. You are given an integer and a tree consisting of nodes. Each node has a value associated with it.
We call a set of tree nodes valid if following conditions are satisfied:
- is non-empty.
- is connected. In other words, if nodes and are in , then all nodes lying on the simple path between and should also be presented in .
- .
Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo ( ).
输入格式
The first line contains two space-separated integers () and ().
The second line contains space-separated positive integers ().
Then the next line each contain pair of integers and () denoting that there is an edge between and . It is guaranteed that these edges form a tree.
输出格式
Print the number of valid sets modulo .
样例 #1
样例输入 #1
1 4
2 1 3 2
1 2
1 3
3 4
样例输出 #1
8
样例 #2
样例输入 #2
0 3
1 2 3
1 2
2 3
样例输出 #2
3
样例 #3
样例输入 #3
4 8
7 8 7 5 4 6 4 10
1 6
1 2
5 8
1 3
3 5
6 7
3 4
样例输出 #3
41
提示
In the first sample, there are exactly 8 valid sets: and . Set is not valid, because the third condition isn't satisfied. Set satisfies the third condition, but conflicts with the second condition.