http://codeforces.com/contest/486/problem/D

题意:给定一棵树,点上有权值,以及d,要求有多少种联通块满足最大值减最小值小于等于d。

思路:枚举i作为最大的点权,然后dfs树规一下,就能得出以这个点为最大值的方案数,因为有权值相等的点,所以我们规定一下,只能从标号小的拓展到标号大的,就不会重复了。

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#define ll long long
const ll Mod=;
int tot,go[],first[],next[],a[],d,n;
ll f[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
}
void add(int x,int y){
insert(x,y);insert(y,x);
}
void dfs(int x,int fa,int fi){
f[x]=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (pur==fa) continue;
if (a[pur]>a[fi]) continue;
if (a[fi]-d>a[pur]) continue;
if (a[fi]==a[pur]&&fi>pur) continue;
dfs(pur,x,fi);
f[x]*=(1LL+f[pur]);
f[x]%=Mod;
}
}
int main(){
d=read();n=read();
for (int i=;i<=n;i++)
a[i]=read();
for (int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
ll ans=;
for (int i=;i<=n;i++){
for (int j=;j<=n;j++) f[j]=;
dfs(i,,i);
ans=(ans+f[i])%Mod;
}
printf("%I64d\n",ans);
}

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

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

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

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

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

  3. Codeforces 486D. Valid Sets

    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 暴力

    D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...

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

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

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

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

  8. codeforces B. Eight Point Sets 解题报告

    题目链接:http://codeforces.com/problemset/problem/334/B 一开始看到题目,有点怯,理解了题目后,其实并不难.这句话是突破口 three distinct ...

  9. Codeforces 425E Sereja and Sets dp

    Sereja and Sets 我们先考虑对于一堆线段我们怎么求最大的不相交的线段数量. 我们先按 r 排序, 然后能选就选. 所以我们能想到我们用$dp[ i ][ j ]$表示已经选了 i 个线段 ...

随机推荐

  1. Activity被回收导致fragment的getActivity为null的解决办法

    这两天一直被这个问题困扰,假如app长时间在后台运行,再点击进入会crash,而且fragment页面有重叠现象,让我十分不爽.研究了一天,终于明白其中的原理并加以解决.解决办法如下: 如果系统内存不 ...

  2. Loading Cargo

    Loading Cargo "Look Stephen, here's a list of the items that need to be loaded onto the ship. W ...

  3. mv,Directory not empty不能目录覆盖

    一.mv /test1/* /test2/test1rm -rf /test1 二. You can however use rsync with the --remove-source-files ...

  4. java进阶计划

    鉴于自己在java 的学习过程中,像是无头苍蝇一样,东扎一把,西戳一下,没有一个明确的方向,也没有一个比较明确的方面,所以有了这个大致的计划. 计划的目标: 1. java本身的目标 对线程(thre ...

  5. Python partial函数

    以前都是摘录的其他网友的博客,很少是自己写的,学习阶段,多多学习.今天开始自己写了,首先写一下刚刚遇到的partial函数. 1.partial函数主要是对参数的改变,假如一个函数有两个参数,而其中一 ...

  6. 找出最小的k个数

    •已知数组中的n个正数,找出其中最小的k个数. •例如(4.5.1.6.2.7.3.8),k=4,则最小的4个数是1,2,3,4 •要求: –高效: –分析时空效率 •扩展:能否设计出适合在海量数据中 ...

  7. 负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体

    负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体 负重前行的婚纱线上路

  8. C++中的重载、覆盖、隐藏

    前几天面试时被问及C++中的覆盖.隐藏,概念基本答不上来,只答了怎么用指针实现多态,也还有遗漏.最终不欢而散.回来后在网上查找学习了一番,做了这个总结.其中部分文字借用了别人的博客,望不要见怪.引用的 ...

  9. vue-cli 脚手架总结

    > vue-cli 的脚手架项目模板有browserify 和 webpack , 现在自己在用的是webpack , 官网给出了两个模板: webpack-simple 和 webpack 两 ...

  10. [原创作品]html css改变浏览器选择文字的背景和颜色

    又很久没有'剥壳'了,最近在为一家公司做一个生产管理解决方案.所以都很忙.今天的话题很简单,就做一个很简单的网页特效.今天偶然浏览到一个网站,他们在选择文字时,样子不是蓝背景和白色字体那么单调,感觉这 ...