参考自:http://blog.csdn.net/cq_phqg/article/details/41040667/
题意:
一颗树,让你找出所有集合S所包含的节点的种数。
集合S定义:如果u,v在S中,则u到v的路径间所有点都要在S中。并且集合S中最大值-最小值的差<=d。
思路:
dp
dp[u]:以u为根节点,包含u的符合条件的种数。
转移:dp[u] = dp[u] * (dp[v]+1) %MOD;(v为u的直接子节点)
然后每个点都dfs一遍即可。
关于去重:如果遇到某节点和最原始父节点的值相等,根据节点的序号,保证只能从大序号到小序号。
code:
/* **********************************************
Created Time: 2014-11-12 14:45:09
File Name : cf.cpp
*********************************************** */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 2005;
const int MOD = 1e9+7;
int head[MAXN];
int val[MAXN];
LL dp[MAXN];
int pp, d, n;
struct PP
{
int v, next;
}edge[2*MAXN];
void addEdge(int u, int v)
{
edge[pp] = (PP){v, head[u]};
head[u] = pp++;
}
void dfs(int u, int par, int root)
{
int next = head[u];
dp[u] = 1;
while(next != -1)
{
PP &e = edge[next];
next = e.next;
if(e.v == par) continue;
if((val[e.v]<val[root]&&val[root]-val[e.v]<=d) ||(val[e.v] == val[root] && root > e.v))
{
dfs(e.v, u, root);
dp[u] = dp[u]*(dp[e.v]+1)%MOD;
}
}
}
void solve()
{
memset(head, -1, sizeof(head));
for(int i = 1;i <= n; i++)
cin>>val[i];
int u, v;
for(int i = 1;i <= n-1; i++)
{
cin>>u>>v;
addEdge(u, v);
addEdge(v, u);
}
LL ans = 0;
for(int i = 1;i <= n; i++)
{
memset(dp, 0, sizeof(dp));
dfs(i, -1, i);
ans = (ans + dp[i]) % MOD;
}
cout<<ans<<endl;
}
int main()
{
//ios::sync_with_stdio(false);
while(cin>>d>>n)
{
pp = 0;
solve();
}
return 0;
}