题目链接:http://codeforces.com/problemset/problem/486/D

题意:

  给你一棵树,n个节点,每个节点的点权为a[i]。

  问你有多少个连通子图,使得子图中的max(a[i]) - min(a[i]) <= d。

  ps.连通子图的定义:

    如果一个点集V为一个连通子图,则对于任意两点a,b∈V,有a到b路径上的所有点u∈V。

题解:

  因为要保证max(a[i]) - min(a[i]) <= d,所以可以人为地选出一个点rt作为点权最大的点。

  这样在求以rt为最大点的连通子图个数时,只用考虑点权不超过a[rt]的点。

  然而如果有两个点i,j的点权相同,分别以i,j作为最大点的两堆子图中会有重复。

  所以可以定义一下:当以rt作为最大点时,所有点权与a[rt]相等的点,它们的节点编号id[i]必须大于id[rt]。

  这样就能避免重复了。

  所以最终答案 = ∑(以i为最大点的连通子图个数)

  求以i为最大点的连通子图个数,只需一遍O(n)的dp就行。

  注意,当前这是一棵无根树。以下所说的“i的子树”意思是:从i出发往叶子方向的那一堆点。

  假设当前以rt作为最大点。  

  则加入连通子图的点i必须满足:

    (1)a[rt]-a[i]<=d(保证满足题目条件)

    (2)a[i]<=a[rt](保证a[rt]为最大点)

    (3)如果a[i]==a[rt] && i!=rt,则要满足rt<i(避免重复计数)

  表示状态:

    dp[i] = numbers

    表示节点i肯定要选,i的子树所构成的合法连通子图个数

  找出答案:

    每次ans += dp[rt]

  如何转移:

    dp[i] = ∏ (dp[son]+1)

  边界条件:

    对于叶子结点leaf: dp[leaf] = 1

  总复杂度O(n^2)。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MAX_N 2005
#define MOD 1000000007 using namespace std; int n,d;
int a[MAX_N];
vector<int> edge[MAX_N]; void read()
{
cin>>d>>n;
for(int i=;i<=n;i++) cin>>a[i];
int x,y;
for(int i=;i<n;i++)
{
cin>>x>>y;
edge[x].push_back(y);
edge[y].push_back(x);
}
} long long dfs(int now,int p,int rt,int mx)
{
if(mx-a[now]>d || a[now]>mx || (a[now]==mx && p!=- && now<rt)) return ;
long long res=;
for(int i=;i<edge[now].size();i++)
{
int temp=edge[now][i];
if(temp!=p) res=res*(dfs(temp,now,rt,mx)+)%MOD;
}
return res;
} void work()
{
long long ans=;
for(int i=;i<=n;i++) ans=(ans+dfs(i,-,i,a[i]))%MOD;
cout<<ans<<endl;
} int main()
{
read();
work();
}

Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】的更多相关文章

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

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

  2. Codeforces 486D. Valid Sets

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

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

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

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

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

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

  7. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  8. Codeforces 442D Adam and Tree dp (看题解)

    Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...

  9. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

随机推荐

  1. oracle海量数据中提升创建索引的速度

    基本信息情况: 数据库版本:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production 操作系统版本:Ce ...

  2. laravel学习之路2: jwt集成

    "tymon/jwt-auth": "^1.0@dev", 执行 composer update 'providers' => [ .... Tymon\ ...

  3. 在Hierarchy面板隐藏物体

    PlantObjPreview.hideFlags = HideFlags.HideInHierarchy;

  4. css3中font-face属性的用法详解

    @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有许 ...

  5. [DBNETLIB][ConnectionOpen(Connect()).]SQL Server 不存在或拒绝访问 数据库错误 解决办法总结

    连接数据库报错:“数据库异常:[DBNETLIB] [ConnectionOpen(Connenct()).] Sqlserver 不存在或拒绝访问” 原因: 1.查看是不是没有在数据库中添加数据库服 ...

  6. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  7. java.time.format.DateTimeFormatter

    Java的日期与时间 DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下.   1.预定义的DateTimeFo ...

  8. 我的Android进阶之旅------>Android字符串资源中的单引号问题error: Apostrophe not preceded by 的解决办法

    刚刚在string字符串资源文件中,写了一个单引号,报错了,错误代码如下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资源文 ...

  9. windows10下载

    http://care.dlservice.microsoft.com/dl/download/F/5/7/F574727C-B145-4A7D-B85B-11C4E8DC894B/9841.0.14 ...

  10. Nodejs课堂笔记-第三课 构建一个nodejs的Docker镜像

    本文由Vikings(http://www.cnblogs.com/vikings-blog/) 原创,转载请标明.谢谢! 因为一直做Linux有关的开发工作,所以不习惯在Windows平台编译和测试 ...