/**********************
* Author:fisty
* Data:2014-11-15
* CF468D
* dfs,tree
* *******************/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define MAX_N 2005
#define MOD 1000000007
vector <int> G[MAX_N];
int a[MAX_N];
typedef long long ll;
int d, n, res;
int dfs(int v, int p, int r){
//排除不可能情况
if(a[v] > a[r] || a[v] < a[r] - d || a[v] == a[r] && v < r)
return 0;
int ans = 1;
for(int i = 0;i < G[v].size(); i++){
int u = G[v][i];//遍历v的相邻节点
if(u == p) continue;
ans = ll(ans) * (1 + dfs(u, v, r)) % MOD;
}
return ans;
}
int main(){
scanf("%d%d", &d, &n);
//输入节点值
for(int i = 1;i <= n; i++)
scanf("%d", &a[i]);
//建图
for(int i = 0;i < n-1; i++){
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
//dfs遍历每一个节点
for(int i = 1;i <= n; i++){
res = (res + dfs(i, 0, i)) % MOD;
}
printf("%d\n", res);
return 0;
}
codeforces 468D Valid Sets
最新推荐文章于 2020-01-27 19:18:23 发布