CF 486D vailid set 树形DP

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 vshould 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 modulo1000000007 (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

input

0 31 2 31 22 3

output

3

input

4 87 8 7 5 4 6 4 101 61 25 81 33 56 73 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.

题意:

给定一棵树,树有点权,现在有树中有多少个有效的集合

有效的集合:

1.集合非空

2.集合是连通的,也就是说集合组成的还是一棵树

3.集合中,最大点权-最下点权<=d

这道题暑假的时候有想过,没有想出来

今天一想,其实就是一道简单的计数问题

由于n很小,O(n^2)是可以的

要max-min<=d

也就是要max<=min+d

dp[i]:i在集合里面,并且集合的最小点权就是i的点权的有效集合的个数

则:ans=sigma(dp[i])

对于一个节点root,我们考虑这个点的点权是他所在的有效集合中的最小点权,并且以root为根开始进行树形DP

如果节点i的点权>=a[root]&& 点权<=a[root]+d

我们就认为root可以扩展到i,不断扩展

并且有dp[u]=dp[u]*(1LL+dp[v])%mod

这样dfs一遍就可以在O(n)算出dp[root]了

以每一个点作为root 来dfs一遍,累加就可以得到ans了

注意一个问题:

有可能a[u]==a[v]

我们以root=u时扩展到v,并且加入了v,算了一遍

然后以root=v时扩展到u,这个时候我们如果把u加入,就会重复计算了

那么在有多个点的点权相等时,我们怎么避免重复计算,只算一次呢?

其实只要我们设一个数组vis[i][j],算第一次的时候我们把数组标记为true,后面就不再加入了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>

#define LL long long

using namespace std;

const int maxn=2010;
const int mod=1e9+7;

LL dp[maxn];
int a[maxn];
bool vis[maxn][maxn];
int sum;
int root;

struct Edge
{
    int to,next;
};
Edge edge[maxn<<1];
int head[maxn];
int tot;

void init()
{
    memset(head,-1,sizeof head);
    tot=0;
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void solve(int ,int d);

int main()
{
    init();
    int n,d;
    scanf("%d %d",&d,&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<n;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        addedge(u,v);
        addedge(v,u);
    }
    solve(n,d);

    return 0;
}

void dfs(int u,int pre)
{
    dp[u]=1;
    for(int i=head[u];~i;i=edge[i].next){
        int v=edge[i].to;

        if(v==pre || a[v]<a[root] || a[v]>sum)
            continue;

        if(a[v]==a[root]){
            if(!vis[v][root]){
                vis[v][root]=true;
                vis[root][v]=true;
                dfs(v,u);
            }
            else
                continue;
        }
        else{
            dfs(v,u);
        }
        dp[u]=dp[u]*(1LL+dp[v])%mod;
    }
}

void solve(int n,int d)
{
    memset(vis,false,sizeof vis);
    LL ans=0;
    for(int i=1;i<=n;i++){
        sum=a[i]+d;
        root=i;
        dfs(root,root);
        ans=(ans+dp[root])%mod;
        ans=(ans+mod)%mod;
        //cout<<dp[root]<<endl;
    }

    printf("%I64d\n",ans);
    return ;
}
时间: 2024-08-06 17:47:50

CF 486D vailid set 树形DP的相关文章

Codeforces 486D Valid Sets 树形dp+计数

题目链接:点击打开链接 题意:给定常数d 和 n,表示n个点的树,每个点有点权 问:有多少个点集使得点集中的点两两可达且最大点权-最小点权<=d 思路: 对于每个点,计算包含这个点且这个点作为点集中的最小点权时的个数,枚举每个点. import java.io.PrintWriter; import java.text.DecimalFormat; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.

CF EDU 1101D GCD Counting 树形DP + 质因子分解

CF EDU 1101D GCD Counting 题意 有一颗树,每个节点有一个值,问树上最长链的长度,要求链上的每个节点的GCD值大于1. 思路 由于每个数的质因子很少,题目的数据200000<2*3*5*7*11*13*17=510510.所以每个节点的质因子个数不多.那么树形DP的时候直接枚举每种因子即可. //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") /

CF 219D Choosing Capital for Treeland 树形DP 好题

一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次

CF 337D Book of Evil 树形DP 好题

Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some p

CF 461B Appleman and Tree 树形DP

Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of k (0 ≤ k < n) edges of Appleman's tree. If Appleman deletes these edges from the tree, then

CF 1000G Two-Paths (树形DP)

题目大意:给你一棵树,点有点权$a_{i}$,边有边权$w_{e}$,定义一种路径称为$2-path$,每条边最多经过2次且该路径的权值为$\sum _{x} a_{x}\;-\;\sum_{e}w_{e}\cdot k_{e}$,$k_{e}$为边的经过次数,一共$Q$次询问,每次查询经过$x,y$的$2-path$权值最大的路径的权值 看题解之前感觉不可做...有点思路但感觉不靠谱,都被我否掉了 LiGuanlin神犇提供了一种树形DP的解法 定义$f[x][0]$表示该子树内,经过$x$点

【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock&#39;s blog】

树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AVL树,线段树.SPLAY树,后缀树等等.. 枚举那么多种数据结构只是想说树方面的内容相当多,本专辑只针对在树上的动态规划,即树形DP.做树形DP一般步骤是先将树转换为有根树,然后在树上进行深搜操作,从子节点或子树中返回信息层层往上更新至根节点.这里面的关键就是返回的信息部分,这个也没一般性的东西可讲

HDU-2196 Computer (树形DP)

最近在看树形DP,这题应该是树形DP的经典题了,写完以后还是有点感觉的.之后看了discuss可以用树分治来做,以后再试一试. 题目大意 找到带权树上离每个点的最远点.︿( ̄︶ ̄)︿ 题解: 对于每一个点的最远点,就是以这个点为根到所有叶子节点的最长距离.但是如果确定根的话,除了根节点外,只能找到每个节点(度数-1)个子树的最大值,剩下一个子树是该节点当前的父亲节点. 所以当前节点的最远点在当前节点子树的所有叶子节点以及父亲节点的最远点上(当父亲节点的最远点不在当前节点的子树上时), 如果父亲节

UVA-01220 Party at Hali-Bula (树形DP+map)

题目链接:https://vjudge.net/problem/UVA-1220 思路: 树形DP模板题,求最大人数很简单,难点在于如何判断最大人数的名单是否有不同的情况: 解决方法是用一个数组f[manx][2]记录该节点是否出场的情况,为真时代表有多种情况; 具体讨论: 当父节点的值加上某个子节点的值时,他的f的情况也和该子节点一样: 当某个节点dp(i, 0) == dp(i, 1), 则该节点以及它的父节点也一定有多种情况(父节点必定取其中之一). Code: 1 #include<bi