D. Valid Sets
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

As you know, an undirected connected graph with n nodes and n - 1 edges
is called a tree. You are given an integer d and a tree consisting of n nodes.
Each node i has a value ai associated
with it.

We call a set S of tree nodes valid if following conditions are satisfied:

  1. S is non-empty.
  2. S is connected. In other words, if nodes u and v are
    in S, then all nodes lying on the simple path between u and v should
    also be presented in S.
  3. .

Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007(109 + 7).

Input

The first line contains two space-separated integers d (0 ≤ d ≤ 2000)
and n (1 ≤ n ≤ 2000).

The second line contains n space-separated positive integers a1, a2, ..., an(1 ≤ ai ≤ 2000).

Then the next n - 1 line each contain pair of integers u and v (1 ≤ u, v ≤ n)
denoting that there is an edge between u and v.
It is guaranteed that these edges form a tree.

Output

Print the number of valid sets modulo 1000000007.

Sample test(s)
input
1 4
2 1 3 2
1 2
1 3
3 4
output
8
input
0 3
1 2 3
1 2
2 3
output
3
input
4 8
7 8 7 5 4 6 4 10
1 6
1 2
5 8
1 3
3 5
6 7
3 4
output
41
Note

In the first sample, there are exactly 8 valid sets: {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {3, 4} and {1, 3, 4}.
Set {1, 2, 3, 4} is not valid, because the third condition isn't satisfied. Set {1, 4} satisfies
the third condition, but conflicts with the second condition.

题意:RT

思路:树形DP。dp[u]表示以u为根。且它的全部子树的点v的权值都不超过它。且满足 w[u]-w[v]<=d 的方案数

            对于每一个点为根这样算一遍就好了,注意减去算重的情况,即相邻多个点的权值相等,能够用点的代号去重(即规定仅仅能从代号大的往小的DP)

Codeforces Round #277 (Div. 2)D(树形DP计数类)的更多相关文章

  1. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  2. Codeforces Round #551 (Div. 2) D 树形dp

    https://codeforces.com/contest/1153/problem/D 题意 一颗有n个点的数,每个点a[i]为0代表取子节点的最小,1代表取最大,然后假设树上存在k个叶子,问你如 ...

  3. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  4. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  5. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...

  6. Codeforces Round #277(Div 2) A、B、C、D、E题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. Calculating Function 水题,判个奇偶即可 #includ ...

  7. AIM Tech Round 3 (Div. 1) (构造,树形dp,费用流,概率dp)

    B. Recover the String 大意: 求构造01字符串使得子序列00,01,10,11的个数恰好为$a_{00},a_{01},a_{10},a_{11}$ 挺简单的构造, 注意到可以通 ...

  8. Educational Codeforces Round 67 E.Tree Painting (树形dp)

    题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...

  9. Codeforces Round #277 (Div. 2) E. LIS of Sequence DP

    E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...

随机推荐

  1. 命令行下修改postgres密码

    1. 修改PostgreSQL数据库默认用户postgres的密码 PostgreSQL数据库创建一个postgres用户作为数据库的管理员,密码随机,所以需要修改密码,方式如下: 步骤一:登录Pos ...

  2. TensorFlow L2正则化

    TensorFlow L2正则化 L2正则化在机器学习和深度学习非常常用,在TensorFlow中使用L2正则化非常方便,仅需将下面的运算结果加到损失函数后面即可 reg = tf.contrib.l ...

  3. [python测试框架学习篇] 分享一个和adb相关的测试框架

    https://testerhome.com/topics/7106   (user: zteandallwinner     password: same to qq ) 264768502 · # ...

  4. pycharm配置 自动运行指定脚本

    配置:RUN -Debug Configuration Script: 脚本的地址 Script Parameters: 脚本的参数 Interpreter: python解释器

  5. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()解决办法

    代码改变世界 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.pre ...

  6. 【Luogu】P2389电脑班的裁员(DP)

    题目链接 sbt交了三遍才过是我的耻辱…… 就是设f[i][j]搞个三重循环DP一下,以上. #include<cstdio> #include<cstdlib> #inclu ...

  7. javaweb-简单的验证码和算术验证码

    我们登陆网站时,每次都会填写一些验证码,这些验证码的作用:防止被恶意攻击,验证码上面的字母数字一般都是随机生成的,因此我们首先要写一个方法生成一个随机的字符串,这里就需要java里面的随机函数Rand ...

  8. FZU Problem 2200 cleaning dp

    Problem Description N个人围成一圈在讨论大扫除的事情,需要选出K个人.但是每个人与他距离为2的人存在矛盾,所以这K个人中任意两个人的距离不能为2,他们想知道共有多少种方法. Inp ...

  9. 刷题总结——book of evil(codefoeces 337D)

    题目: description Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This ar ...

  10. 算法复习——哈希表+折半搜索(poj2549)

    搬讲义~搬讲义~ 折半搜索感觉每次都是打暴力时用的啊2333,主要是用于降次··当复杂度为指数级别时用折半可以减少大量复杂度··其实专门考折半的例题并不多···一般都是中途的一个小优化··· 然后折半 ...