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

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

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

  1. #include<algorithm>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<iostream>
  6. #define ll long long
  7. const ll Mod=;
  8. int tot,go[],first[],next[],a[],d,n;
  9. ll f[];
  10. int read(){
  11. int t=,f=;char ch=getchar();
  12. while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
  13. while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
  14. return t*f;
  15. }
  16. void insert(int x,int y){
  17. tot++;
  18. go[tot]=y;
  19. next[tot]=first[x];
  20. first[x]=tot;
  21. }
  22. void add(int x,int y){
  23. insert(x,y);insert(y,x);
  24. }
  25. void dfs(int x,int fa,int fi){
  26. f[x]=;
  27. for (int i=first[x];i;i=next[i]){
  28. int pur=go[i];
  29. if (pur==fa) continue;
  30. if (a[pur]>a[fi]) continue;
  31. if (a[fi]-d>a[pur]) continue;
  32. if (a[fi]==a[pur]&&fi>pur) continue;
  33. dfs(pur,x,fi);
  34. f[x]*=(1LL+f[pur]);
  35. f[x]%=Mod;
  36. }
  37. }
  38. int main(){
  39. d=read();n=read();
  40. for (int i=;i<=n;i++)
  41. a[i]=read();
  42. for (int i=;i<n;i++){
  43. int x=read(),y=read();
  44. add(x,y);
  45. }
  46. ll ans=;
  47. for (int i=;i<=n;i++){
  48. for (int j=;j<=n;j++) f[j]=;
  49. dfs(i,,i);
  50. ans=(ans+f[i])%Mod;
  51. }
  52. printf("%I64d\n",ans);
  53. }

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. 抗忙,,建个MAVEN的私服仓库-NEXUS

    公司最近需求越来越上轨道,MAVEN的私服仓库-NEXUS构架起来哟.. 参考文档URL: http://www.linuxidc.com/Linux/2011-07/39578p3.htm http ...

  2. pfsense 2.2RC版本应用

    为什么要上RC版本呢?因为华硕主板有一个RTL8111G驱动在2.15中还没有识别.... 公司双线WAN,一个PPPOE一个静态IP. 开了端口转发, 要求对不同的IP进行相关限速, 到达指定网站用 ...

  3. 通过JCONSOLE监控TOMCAT的JVM使用情况

    这个也是要学入一下,JVMr 虚拟机原理不可少. 参考配置URL“: http://blog.163.com/kangle0925@126/blog/static/277581982011527723 ...

  4. 浅谈PCB敷铜的“弊与利”

    敷铜作为PCB设计的一个重要环节,不管是国产的青越锋PCB设计软件,还国外的一些Protel,PowerPCB都提供了智能敷铜功能,那么怎样才能敷好铜,我将自己一些想法与大家一起分享,希望能给同行带来 ...

  5. 改进RazorPad

    从Git 上下载了作者的源码后,感觉用起来挺别扭,而且还要BUG............ 经过“篡改”后,好用多了,呵呵..

  6. 数据库范式(1NF 2NF 3NF BCNF)详解

    数据库的设计范式是数据库设计所需要满足的规范,满足这些规范的数据库是简洁的.结构明晰的,同时,不会发生插入(insert).删除(delete)和更新(update)操作异常.反之则是乱七八糟,不仅给 ...

  7. UVA 825 Walking on the Safe Side(记忆化搜索)

      Walking on the Safe Side  Square City is a very easy place for people to walk around. The two-way ...

  8. 找工作笔试面试那些事儿(10)---SQL语句总结

    SQL语句中常用关键词及其解释如下: 1)SELECT 将资料从数据库中的表格内选出,两个关键字:从 (FROM) 数据库中的表格内选出 (SELECT).语法为 SELECT "栏位名&q ...

  9. FLEX中Tree默认展开全部节点

    这里分两种情况,一种是数据源在MXML文件里,如: <mx:XML id="treeXML" format="e4x"> <root> ...

  10. Linux文件 I/O 介绍

    Linux文件 I/O 介绍 1. Linux系统调用 Linux系统调用(system call)是指操作系统提供给用户程序的一组"特殊接口",用户程序可以通过这组"特 ...