D. Valid Sets
 

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
 
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.

题意:给你一个n点的树,和每个点的权值,问你多少种子树满足(最大权值点-最小权值点)<=d

题解:定义dp[i]表示以i为最小权值根节点的子树方案数,注意维护此条件

于是答案就是  ∑dp[i] %mod (1<=i<=n);

///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define meminf(a) memset(a,127,sizeof(a)); inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
#define maxn 2000+50
#define mod 1000000007
#define inf 1000000007
int d,n,a[maxn],vis[maxn];
vector<int >G[maxn];
ll dp[maxn];//以i为最小根节点,的方案数
void dfs(int x,int pre){
dp[x]=;vis[x]=;
for(int i=;i<G[x].size();i++){
if(!vis[G[x][i]]){
if(a[G[x][i]]<a[pre]||a[G[x][i]]>a[pre]+d)continue;
if(a[G[x][i]]==a[pre]&&G[x][i]<pre)continue;
dfs(G[x][i],pre);
dp[x]=(dp[x]*(dp[G[x][i]]+))%mod;
}
}
} int main(){
d=read(),n=read();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}int u,v;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
G[u].pb(v);G[v].pb(u);
}ll ans=;
for(int i=;i<=n;i++){
mem(dp);mem(vis);
dfs(i,i);
ans=(ans+dp[i])%mod;
}
cout<<ans<<endl;
return ;
}

代码

Codeforces Round #277 (Div. 2) D. Valid Sets DP的更多相关文章

  1. Codeforces Round #277 (Div. 2) D. Valid Sets (DP DFS 思维)

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. Codeforces Round #277 (Div. 2) D. Valid Sets 暴力

    D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...

  3. Codeforces Round #277 (Div. 2)D(树形DP计数类)

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

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

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

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

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

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

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

  7. 套题 Codeforces Round #277 (Div. 2)

    A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...

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

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

  9. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

随机推荐

  1. [ POI 2011 ] Party

    \(\\\) \(Description\) 给定一张 \(N\ (\ N\equiv 0\pmod{3}\ )\) 个节点,,\(M\)条边的图,并且保证该图存在一个大小至少为\(\frac{2}{ ...

  2. DetachedCriteria和Criteria的使用方法

    DetachedCriteria和Criteria的使用方法 /*  * 下载统计  * @return  */ public String downloadStatistics(){  logger ...

  3. html5——3D转换

    角度旋转 rotateX:默认以center绕x轴旋转 rotateY:默认以center绕y轴旋转 rotateZ:默认以cente绕z轴r旋转 //rotateX原点为center==>正值 ...

  4. Detectron-MaskRCnn: 用于抠图的FCNN

    市面上暂时还没有找到可以在消费机显卡上实时运行的MaskRCnn,TensorFlow即使是C++版本训练在coco数据集上的模型也是慢的要死,最后不堪忍受,只能放弃. 经历了一些列fuckingDo ...

  5. 【sqli-labs】 less57 GET -Challenge -Union -14 queries allowed -Variation4 (GET型 挑战 联合查询 只允许14次查询 变化4)

    双引号闭合 http://192.168.136.128/sqli-labs-master/Less-57/?id=1"%23 和less56一样查数据

  6. PHP 之转换excel表格中的经纬度

    <?php set_time_limit(0); include './plugin/PHPExcel/PHPExcel.php'; include './plugin/PHPExcel/PHP ...

  7. 用u盘安装黑苹果10.12.3

    链接: https://pan.baidu.com/s/1eR9GgwE 密码: rubh 主机和显示器必须是数字口连接,如dvi.displayport,VGA不能进安装界面 下载苹果镜像文件10. ...

  8. Perl Connect to Database without password as sysdba

    #!/oracle/product/11g/db/perl/bin/perl use lib '/oracle/product/11g/db/perl/lib/site_perl/5.10.0'; u ...

  9. ES6学习历程(字符串的扩展)

    字符串的扩展 在看这一节的时候前半部分写的都是关于unicode的内容,我个人感觉这部分在实际的开发中用的很少,所以不打算在做记录,等届时用到再有针对性的看,所以就将在ES6里面关于字符串操作的一些新 ...

  10. gitblit 搭建本地git服务器

    本文主要描述gitblit搭建本地服务器