D. Valid Sets

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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:

S is non-empty.
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.
.

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.

Examples

Input

Copy

1 4

2 1 3 2

1 2

1 3

3 4

Output

8

Input

Copy

0 3

1 2 3

1 2

2 3

Output

3

Input

Copy

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

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.

题解

直接统计不好统计,所以考虑把方案分类。

按照最大值点对方案分类,枚举一个最大值点,让这一个点在方案中,这样就确定了方案能包含的连通块。

设\(dp[i]\)表示一定包含i这个点且i这个点是方案中权值最大的点的方案,有

\[dp[i] = \prod (dp[son_i] + 1)
\]

这样会计算重。因为最大值点所确定的连通块里,可能有多个与最大值点权值相同的点,每个点都会算一遍。

只算一个,然后乘以连通的相同值点的个数?

不!别忘了我们\(dp\)的时候,要求选定的点必须在方案中。必定包含不同的相同最大权值点的方案并不是一一对应的。

怎么办?

继续把方案分类。

在这个包含多个最大值点的极大连通块里,设有\(k\)个相同的最大值点。

设最大的相同点编号为\(x\),\(dp\)必定包含点x的集合就行了。

这就相当于,我们只走比他编号小的相同权值的点。

两次使用最大值分类方案的神题Orz

被long long 和 MOD卡了两次。。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
inline long long max(long long a, long long b){return a > b ? a : b;}
inline long long min(long long a, long long b){return a < b ? a : b;}
inline long long abs(long long x){return x < 0 ? -x : x;}
inline void swap(long long &x, long long &y){long long tmp = x;x = y;y = tmp;}
inline void read(long long &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f;
const long long MAXN = 2000 + 10;
const long long MOD = 1000000007;
struct Edge
{
long long u,v,nxt;
Edge(long long _u, long long _v, long long _nxt){u = _u, v =_v, nxt = _nxt;}
Edge(){}
}edge[MAXN << 1];
long long head[MAXN], cnt, n, val[MAXN], d, dp[MAXN], ans;
inline void insert(long long a, long long b)
{
edge[++ cnt] = Edge(a, b, head[a]), head[a] = cnt;
}
void dfs(long long x, long long pre, long long root)
{
dp[x] = 1;
for(long long pos = head[x];pos;pos = edge[pos].nxt)
{
long long v = edge[pos].v;
if(v == pre || val[root] - val[v] < 0 || val[root] - val[v] > d || (val[v] == val[root] && v > root)) continue;
dfs(v, x, root);
dp[x] *= (dp[v] + 1), dp[x] %= MOD;
}
}
int main()
{
read(d), read(n);
for(long long i = 1;i <= n;++ i) read(val[i]);
for(long long i = 1;i < n;++ i)
{
long long tmp1, tmp2;
read(tmp1), read(tmp2);
insert(tmp1, tmp2), insert(tmp2, tmp1);
}
for(long long i = 1;i <= n;++ i)
{
memset(dp, 0, sizeof(dp));
dfs(i, -1, i);
ans += dp[i] % MOD;
if(ans >= MOD) ans -= MOD;
}
printf("%I64d", ans);
return 0;
}

Codeforces 486D. Valid Sets的更多相关文章

  1. Codeforces 486D Valid Sets (树型DP)

    题目链接 Valid Sets 题目要求我们在一棵树上计符合条件的连通块的个数. 满足该连通块内,点的权值极差小于等于d 树的点数满足 n <= 2000 首先我们先不管这个限制条件,也就是先考 ...

  2. Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】

    题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...

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

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

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

  6. Codeforces 486D D. Valid Sets

    http://codeforces.com/contest/486/problem/D 题意:给定一棵树,点上有权值,以及d,要求有多少种联通块满足最大值减最小值小于等于d. 思路:枚举i作为最大的点 ...

  7. codeforces 486 D. Valid Sets(树形dp)

    题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...

  8. Codeforces 722D. Generating Sets

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. codeforces 722D Generating Sets 【优先队列】

    You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...

随机推荐

  1. python对urlEncode进行解码

    利用python自带的urlib进行编码和解码,没有什么问题.(https://www.hustyx.com/python/) 但如果是用url编码工具(http://tool.chinaz.com/ ...

  2. Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.widget.CheckedTextView

    困扰了我一天啊 终于吧 这个大bug  给解决掉了 可能是 当时懵逼了  竟然忘记重新构造了!!尴尬了 直接把项目的 build  文件删除重新构造了一边!!

  3. 校园商铺-2Logback配置与使用-3验证配置

    1. 验证logback配置 1.1. 启动tomcat,得到CATALINA_BASE地址: 1.2 访问接口,查看日志 浏览器打开http://localhost:18080/o2o/supera ...

  4. requests 返回 521

    网页端抓数据免不了要跟JavaScript打交道,尤其是JS代码有混淆,对cookie做了手脚.找到cookie生成的地方要费一点时间. 那天碰到这样一个网页,用浏览器打开很正常.然而用request ...

  5. Linux 后台运行python .sh等程序,以及查看和关闭后台运行程序操作

    1.运行.sh文件 直接用./sh 文件就可以运行,但是如果想后台运行,即使关闭当前的终端也可以运行的话,需要nohup命令和&命令. (1)&命令 功能:加在一个命令的最后,可以把这 ...

  6. dashboard服务

    1.上传镜像,并导入,打标签 2.创建dashboard的deployment和service apiVersion: extensions/v1beta1 kind: Deployment meta ...

  7. 17.splash_case03

    # python执行lua脚本 import requests from urllib.parse import quote lua = ''' function main(splash) retur ...

  8. wpf 让正执行的程序暂停几秒钟

    public static class DispatcherHelper     {         [SecurityPermissionAttribute(SecurityAction.Deman ...

  9. C++函数或者命名空间前面加::

    命名空间和函数前面加上:: 经常看到命名空间前就只有:: 比如  ::test;这种代表是全局的test 比如  ::CreateDirectory(..),代表使用系统API也就是全局的 避免使用到 ...

  10. [NOIP2019模拟赛]夹缝

    夹缝 问题描述: 二维空间内有两面相对放置的,向无限远延伸的镜子,以镜子的方向及其法向量建立坐标系,我们选定一个法向量方向下面称“上”.在镜子上的整数位置,存在着一些传感器,传感器不影响光线的反射,光 ...