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

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 42 1 3 21 21 33 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);

///1085422276
#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=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){
        if(ch==‘-‘)f=-1;ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘){
        x=x*10+ch-‘0‘;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]=1;vis[x]=1;
   for(int i=0;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]]+1))%mod;
      }
   }
}

int main(){
    d=read(),n=read();
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }int u,v;
    for(int i=1;i<n;i++){
        scanf("%d%d",&u,&v);
        G[u].pb(v);G[v].pb(u);
    }ll ans=0;
    for(int i=1;i<=n;i++){
        mem(dp);mem(vis);
        dfs(i,i);
        ans=(ans+dp[i])%mod;
    }
    cout<<ans<<endl;
  return 0;
}

代码

时间: 2024-09-05 07:21:33

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

Codeforces Round #277 (Div. 2) LIS of Sequence Dp

题意: 给出一个序列,问每个位置的元素,分别属于哪一类的东西.第一类 没有出现在任何的上升子序列中. 第三类 出现在所有上升子序列中 .第二类 就是剩下的了.. 求两个东西 ,  dp[i] 表示 从1到 i 最长上升子序列的长度,dp1[i]表示从i到n 最长上升子序列的长度. 设原序列最长上升子序列长度为len 1. 若dp[i]+dp1[i] - 1 != len , 他就属于第一类. 2. 若 t  = dp[i] 的 t 出现了不止一次,且不属于第一类,那他就是第二类. 剩下的就是第三

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

门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; LL n ; int main () { while ( ~scanf ( "%I64d" , &n )

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

题目传送门 1 /* 2 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 9:14:02 7 File Name :B.cpp 8 *************************************************/ 9 10 #include

Codeforces Round #277 (Div. 2) d

/**  * @brief Codeforces Round #277 (Div. 2) d  * @file d.cpp  * @author 面码  * @created 2014/11/17 14:53  * @edited  2014/11/17 14:53  * @type dfs dp  *   *  */ #include <cstdio> #include <vector> #define MOD 1000000007 #define MAXN 2014 using

Codeforces Round #277 (Div. 2) c

/**  * @brief Codeforces Round #277 (Div. 2) c  * @file c.c  * @author 面码  * @created 2014/11/14 13:39  * @edited  2014/11/14 13:39  * @type greedy  *  */ #include <stdio.h> #define MAXN 100010 #define MAXC 26 #define max(a, b)  ((a) > (b) ? (a) 

Codeforces Round #277 (Div. 2) a

/**  * @brief Codeforces Round #277 (Div. 2) a  * @author xiyan  * @created 2014/11/13 11:23  * @edited  2014/11/13 11:24  * @type math   *   *  */ #include <stdio.h> long long int a; int main() {     scanf("%I64d", &a);     printf(&qu

Codeforces Round #277 (Div. 2) b

/**  * @brief Codeforces Round #277 (Div. 2) b  * @author 面码  * @created 2014/11/13 14:01  * @edited  2014/11/13 14:01  * @type greedy  * @TODO less space and time cost with bitmap   *   *  */ #include <stdio.h> #define MAXN 110 //int a[MAXN][MAXN];

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿