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. C#使用.net.mail配置163邮箱报错:不允许使用邮箱名称。 服务器响应为:authentication is required,smtp9,DcCowABHK4UYE11W2k6fAQ--.52196S2 1448940312

    client.UseDefaultCredentials = true; 要放在 client.Credentials = new NetworkCredential("用户名", ...

  2. 利用Connect By构造数列

    ,) yymm ;

  3. QT5 r 加入qwtplot3d 三维库

          qwtplot3d是基于QtOpenGL开发的,也是qwt库的三维库,我使用的是qwtplot3d-0.2.7.zip版本.   步骤跟编译qwt库一样(不明白可以看回前面写的一篇文章“Q ...

  4. [CSAPP笔记][第二章信息的表示和处理]

    信息的表示和处理 2.1 信息存储 机器级程序将存储器视为一个非常大的字节数组,称为虚拟存储器. 存储器的每个字节由一个唯一的数字表示,称为它的地址 所有可能地址的集合称为虚拟地址空间 2.1.1 十 ...

  5. Some good iOS questions

    这里,我列举了一些在Stackoverflow中一些比较好的关于iOS的问题.大部分我列举的问题都是关于Objective C.所有问题中,我比较喜欢“为什么”这一类型的问题. 问题 1. What’ ...

  6. indexOf()忽略大小写方法

    indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置.如果没有出现,则输出-1. indexOf() 方法对大小写敏感!所以要检索字符串且忽略大小写的时候,可以先把字符串转换成全部 ...

  7. 老生常谈的Javascript作用域问题

    在前端学习中,作用域这个问题一直被广泛提起,什么是作用域,什么又是作用域链?在Javascript中,怎么去理解这些概念都是学好这门语言的关键,所以在学习前端开发的过程中,我需要也很有必要去学习和总结 ...

  8. memcached session共享

    http://www.baidu.com/s?wd=memcached%20session%E5%85%B1%E4%BA%AB&rsv_spt=1&issp=1&f=8& ...

  9. repeater截取字数

    <%# Eval("ArticleName").ToString().Length > 14 ? Eval("ArticleName").ToStr ...

  10. 创建存储过程和函数【weber出品必属精品】

    一.什么是存储过程和函数 1. 是被命名的pl/sql块 2. 被称之为pl/sql子程序 3. 与匿名块类似,有块结构: 声明部分是可选的(没有declare关键字) 必须有执行部分 可选的异常处理 ...