CF486D-Valid Sets
题目
给出一个\(n\)个点的树,每个点有权值\(a_i\),再给出一个\(d\),问有多少个非空点集满足:
- 点集在树上构成联通子图
- \[\max _{v\in S}a_v -\min _{v\in S}\le d$$,即集合内权值最大减最小在$d$以内
\]
分析
首先如何求树上联通子图的总数?设\(f_i\)表示包含\(i\)点的联通子图个数,那么有:
\]
即考虑是否包含每个子树中的集合,如果不包含就给其他的乘1,否则就乘上子树中的集合个数。
如果有\(d\)的条件怎么办呢?
为了不算重,我们对每个点\(x\)做树形dp,强制\(a_x\)为权值最小的,那么只进入\(a_x\le a_v\le a_x+d\)的子树进行计算。但这样依然会算重,原因是如果有相同权值的点,那么可能会在这些点都统计到同一个集合。
于是我们给它定序。重复计算其实就是没有一个顺序来区分这些集合。所以当遇到\(a_x=a_v\)的时候,只有\(v>x\)我们才走进去,这样就相当于给集合中相同最小权值的点定序,去除了重复的情况。
代码
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long giant;
int read() {
int x=0,f=1;
char c=getchar();
for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=2e3+1;
const int q=1e9+7;
int n,d,a[maxn],f[maxn],ans=0;
vector<int> g[maxn];
inline void add(int x,int y) {g[x].push_back(y);}
inline int Plus(int x,int y) {return ((giant)x+(giant)y)%q;}
inline int Multi(int x,int y) {return (giant)x*y%q;}
void dfs(int x,int fa,int id,int lim) {
int &fx=f[x]=1;
for (int v:g[x]) if (v!=fa && ((lim<a[v] && a[v]<=lim+d) || (lim==a[v] && v>id))) dfs(v,x,id,lim),fx=Multi(f[x],f[v]+1);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
d=read(),n=read();
for (int i=1;i<=n;++i) a[i]=read();
for (int i=1;i<n;++i) {
int x=read(),y=read();
add(x,y),add(y,x);
}
for (int i=1;i<=n;++i) {
memset(f,0,sizeof f);
dfs(i,i,i,a[i]);
ans=Plus(ans,f[i]);
}
printf("%d\n",ans);
return 0;
}
CF486D-Valid Sets的更多相关文章
- 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 ...
- 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 ...
- Codeforces 486D Valid Sets (树型DP)
题目链接 Valid Sets 题目要求我们在一棵树上计符合条件的连通块的个数. 满足该连通块内,点的权值极差小于等于d 树的点数满足 n <= 2000 首先我们先不管这个限制条件,也就是先考 ...
- 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 ...
- Codeforces 486D. Valid Sets
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 486D D. Valid Sets
http://codeforces.com/contest/486/problem/D 题意:给定一棵树,点上有权值,以及d,要求有多少种联通块满足最大值减最小值小于等于d. 思路:枚举i作为最大的点 ...
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...
- 【【henuacm2016级暑期训练】动态规划专题 N】Valid Sets
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 给你一棵树. 让你统计其中子树T的数量. 这个子树T要满足最大值和最小值之差小于等于d 树形DP 可以枚举点root为子树的根. 统 ...
- codeforces 486 D. Valid Sets(树形dp)
题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...
- 2016 Google code jam 答案
二,RoundC import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundE ...
随机推荐
- 20145226夏艺华 《Java程序设计》预备作业3
安装虚拟机 上学期开学的时候就安装了Linux虚拟机,由于我的是Mac OS,所以和windows下的安装有所不同. 我使用的是VirtualBoxVM虚拟机,稳定性还不错,需要的同学可以从https ...
- hasOwnProperty()函数
hasOwnProperty()函数的返回值为Boolean类型.如果对象object具有名称为propertyName的属性,则返回true,否则返回false 例子: function Site( ...
- vector的二维用法+前缀和
题目链接:https://codeforces.com/contest/1082/problem/C(C. Multi-Subject Competition) A multi-subject com ...
- 北京Uber优步司机奖励政策(4月15日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【转载】关于RenderTarget的注意事项
原文:关于RenderTarget的注意事项 1. 设置一个RenderTarget会导致viewport变成跟RenderTarget一样大 2. 反锯齿类型必须跟DepthStencilBuffe ...
- 一篇文章帮你梳理清楚API设计时需要考虑的几个关键点
本文作者是Enchant的架构师,他最近研究了Netflix.SoundCloud.谷歌.亚马逊.Spotify等公司的微服务实践,并根据自己的理解总结出了一套适用于现代Web和云技术的微服务实战经验 ...
- day 8 递归
版本1) 求5!while # 5! = 5*4*3*2*1 # 4!= 4*3*2*1 i = 1 result = 1 while i <= 5: result = result * i ...
- day3 前奏
1.第1个c语言 编辑---编译----运行 python@ubuntu:~/Desktop/pythons06$ vim -第1个c语言.c #include<stdio.h> int ...
- CF 1083 C. Max Mex
C. Max Mex https://codeforces.com/contest/1083/problem/C 题意: 一棵$n$个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~ ...
- Nginx入门篇(三)之虚拟主机配置
一.虚拟主机概念 所谓虚拟主机,在Web服务当中就是一个独立的网站站点,这个站点对应独立的域名(也有可能是IP或者端口),具有独立的程序和资源目录,可以独立地对外提供服务供用户访问. 这个独立的站点在 ...