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 vshould 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 modulo1000000007 (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. 1 4
    2 1 3 2
    1 2
    1 3
    3 4
output
  1. 8
input
  1. 0 3
    1 2 3
    1 2
    2 3
output
  1. 3
input
  1. 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
  1. 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.

题意:

给定一棵树,树有点权,现在有树中有多少个有效的集合

有效的集合:

1.集合非空

2.集合是连通的,也就是说集合组成的还是一棵树

3.集合中,最大点权-最下点权<=d

这道题暑假的时候有想过,没有想出来

今天一想,其实就是一道简单的计数问题

由于n很小,O(n^2)是可以的

要max-min<=d

也就是要max<=min+d

dp[i]:i在集合里面,并且集合的最小点权就是i的点权的有效集合的个数

则:ans=sigma(dp[i])

对于一个节点root,我们考虑这个点的点权是他所在的有效集合中的最小点权,并且以root为根开始进行树形DP

如果节点i的点权>=a[root]&& 点权<=a[root]+d

我们就认为root可以扩展到i,不断扩展

并且有dp[u]=dp[u]*(1LL+dp[v])%mod

这样dfs一遍就可以在O(n)算出dp[root]了

以每一个点作为root 来dfs一遍,累加就可以得到ans了

注意一个问题:

有可能a[u]==a[v]

我们以root=u时扩展到v,并且加入了v,算了一遍

然后以root=v时扩展到u,这个时候我们如果把u加入,就会重复计算了

那么在有多个点的点权相等时,我们怎么避免重复计算,只算一次呢?

其实只要我们设一个数组vis[i][j],算第一次的时候我们把数组标记为true,后面就不再加入了

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5.  
  6. #define LL long long
  7.  
  8. using namespace std;
  9.  
  10. const int maxn=;
  11. const int mod=1e9+;
  12.  
  13. LL dp[maxn];
  14. int a[maxn];
  15. bool vis[maxn][maxn];
  16. int sum;
  17. int root;
  18.  
  19. struct Edge
  20. {
  21. int to,next;
  22. };
  23. Edge edge[maxn<<];
  24. int head[maxn];
  25. int tot;
  26.  
  27. void init()
  28. {
  29. memset(head,-,sizeof head);
  30. tot=;
  31. }
  32.  
  33. void addedge(int u,int v)
  34. {
  35. edge[tot].to=v;
  36. edge[tot].next=head[u];
  37. head[u]=tot++;
  38. }
  39.  
  40. void solve(int ,int d);
  41.  
  42. int main()
  43. {
  44. init();
  45. int n,d;
  46. scanf("%d %d",&d,&n);
  47. for(int i=;i<=n;i++){
  48. scanf("%d",&a[i]);
  49. }
  50. for(int i=;i<n;i++){
  51. int u,v;
  52. scanf("%d %d",&u,&v);
  53. addedge(u,v);
  54. addedge(v,u);
  55. }
  56. solve(n,d);
  57.  
  58. return ;
  59. }
  60.  
  61. void dfs(int u,int pre)
  62. {
  63. dp[u]=;
  64. for(int i=head[u];~i;i=edge[i].next){
  65. int v=edge[i].to;
  66.  
  67. if(v==pre || a[v]<a[root] || a[v]>sum)
  68. continue;
  69.  
  70. if(a[v]==a[root]){
  71. if(!vis[v][root]){
  72. vis[v][root]=true;
  73. vis[root][v]=true;
  74. dfs(v,u);
  75. }
  76. else
  77. continue;
  78. }
  79. else{
  80. dfs(v,u);
  81. }
  82. dp[u]=dp[u]*(1LL+dp[v])%mod;
  83. }
  84. }
  85.  
  86. void solve(int n,int d)
  87. {
  88. memset(vis,false,sizeof vis);
  89. LL ans=;
  90. for(int i=;i<=n;i++){
  91. sum=a[i]+d;
  92. root=i;
  93. dfs(root,root);
  94. ans=(ans+dp[root])%mod;
  95. ans=(ans+mod)%mod;
  96. //cout<<dp[root]<<endl;
  97. }
  98.  
  99. printf("%I64d\n",ans);
  100. return ;
  101. }

CF 486D vailid set 树形DP的更多相关文章

  1. CF 274B Zero Tree 树形DP

    A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following cond ...

  2. CF 219D 树形DP

    CF 219D [题目链接]CF 219D [题目类型]树形DP &题意: 给一个n节点的有向无环图,要找一个这样的点:该点到其它n-1要逆转的道路最少,(边<u,v>,如果v要到 ...

  3. CF EDU 1101D GCD Counting 树形DP + 质因子分解

    CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000&l ...

  4. CF 337D Book of Evil 树形DP 好题

    Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...

  5. CF 461B Appleman and Tree 树形DP

    Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...

  6. CF 219D Choosing Capital for Treeland 树形DP 好题

    一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) ...

  7. CF 463A && 463B 贪心 && 463C 霍夫曼树 && 463D 树形dp && 463E 线段树

    http://codeforces.com/contest/462 A:Appleman and Easy Task 要求是否全部的字符都挨着偶数个'o' #include <cstdio> ...

  8. CF F - Tree with Maximum Cost (树形DP)给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大。输出最大的值。

    题目意思: 给出你一颗带点权的树,dist(i, j)的值为节点i到j的距离乘上节点j的权值,让你任意找一个节点v,使得dist(v, i) (1 < i < n)的和最大.输出最大的值. ...

  9. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

随机推荐

  1. sessionStorage和localStorage中 存储

    转:http://my.oschina.net/crazymus/blog/371757 sessionStorage只在页面打开是起作用, localStorage关闭页面后仍然起作用. 有时候,我 ...

  2. GNU C 扩展(转)

    GNU CC 是一个功能非常强大的跨平台 C 编译器,它对 C 语言提供了很多扩展,这些扩展对优化.目标代码布局.更安全的检查等方面提供了很强的支持.这里对支持支持 GNU 扩展的 C 语言成为 GN ...

  3. Java——正则表达式(字符串操作)

     public class Test1 { /* * 正则表达式:对字符串的常见操作: * 1.匹配: *  其实是用的就是string类中的matches(匹配)方法. * 2.切割 *  其实 ...

  4. Hibernate之:各种主键生成策略与配置详解

    1.assigned 主键由外部程序负责生成,在 save() 之前必须指定一个.Hibernate不负责维护主键生成.与Hibernate和底层数据库都无关,可以跨数据库.在存储对象前,必须要使用主 ...

  5. php mcrypt 完全安装

    今天安装完 PHP ,访问某个功能时,  /var/log/httpd/error_log  中报如下错误: PHP Fatal error:  Call to undefined function ...

  6. SqlBulkCopy 批量插入数据库

    /// <summary> /// 批量插入 注:DT的tableName为要更新的数据库表名,DT的列名和数据库一致 /// </summary> /// <param ...

  7. unity, 搜索组件

    Hierarchy的搜索栏中既可以搜节点名,也可以搜组件名.

  8. Openjudge计算概论——数组逆序重放【递归练习】

    /*===================================== 数组逆序重放 总时间限制:1000ms 内存限制:65536kB 描述 将一个数组中的值按逆序重新存放. 例如,原来的顺 ...

  9. wikioi 1012最大公约数和最小公倍数【根据最大公约数和最小公倍数求原来的两个数a、b】

    /*====================================================================== 题目描述 输入二个正整数x0,y0(2<=x0& ...

  10. jquery操作html data-* 属性的坑