日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 486D. Valid Sets

發布時間:2025/5/22 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 486D. Valid Sets 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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; }

轉載于:https://www.cnblogs.com/huibixiaoxing/p/8535927.html

總結

以上是生活随笔為你收集整理的Codeforces 486D. Valid Sets的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。