Codeforces 486D D. Valid Sets
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的更多相关文章
- codeforces 486 D. Valid Sets(树形dp)
题目链接:http://codeforces.com/contest/486/problem/D 题意:给出n个点,还有n-1条边的信息,问这些点共能构成几棵满足要求的树,构成树的条件是. 1)首先这 ...
- Codeforces 486D Valid Sets (树型DP)
题目链接 Valid Sets 题目要求我们在一棵树上计符合条件的连通块的个数. 满足该连通块内,点的权值极差小于等于d 树的点数满足 n <= 2000 首先我们先不管这个限制条件,也就是先考 ...
- Codeforces 486D. Valid Sets
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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 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:Tree dp【n遍O(n)的dp】
题目链接:http://codeforces.com/problemset/problem/486/D 题意: 给你一棵树,n个节点,每个节点的点权为a[i]. 问你有多少个连通子图,使得子图中的ma ...
- codeforces B. Eight Point Sets 解题报告
题目链接:http://codeforces.com/problemset/problem/334/B 一开始看到题目,有点怯,理解了题目后,其实并不难.这句话是突破口 three distinct ...
- Codeforces 425E Sereja and Sets dp
Sereja and Sets 我们先考虑对于一堆线段我们怎么求最大的不相交的线段数量. 我们先按 r 排序, 然后能选就选. 所以我们能想到我们用$dp[ i ][ j ]$表示已经选了 i 个线段 ...
随机推荐
- 抗忙,,建个MAVEN的私服仓库-NEXUS
公司最近需求越来越上轨道,MAVEN的私服仓库-NEXUS构架起来哟.. 参考文档URL: http://www.linuxidc.com/Linux/2011-07/39578p3.htm http ...
- pfsense 2.2RC版本应用
为什么要上RC版本呢?因为华硕主板有一个RTL8111G驱动在2.15中还没有识别.... 公司双线WAN,一个PPPOE一个静态IP. 开了端口转发, 要求对不同的IP进行相关限速, 到达指定网站用 ...
- 通过JCONSOLE监控TOMCAT的JVM使用情况
这个也是要学入一下,JVMr 虚拟机原理不可少. 参考配置URL“: http://blog.163.com/kangle0925@126/blog/static/277581982011527723 ...
- 浅谈PCB敷铜的“弊与利”
敷铜作为PCB设计的一个重要环节,不管是国产的青越锋PCB设计软件,还国外的一些Protel,PowerPCB都提供了智能敷铜功能,那么怎样才能敷好铜,我将自己一些想法与大家一起分享,希望能给同行带来 ...
- 改进RazorPad
从Git 上下载了作者的源码后,感觉用起来挺别扭,而且还要BUG............ 经过“篡改”后,好用多了,呵呵..
- 数据库范式(1NF 2NF 3NF BCNF)详解
数据库的设计范式是数据库设计所需要满足的规范,满足这些规范的数据库是简洁的.结构明晰的,同时,不会发生插入(insert).删除(delete)和更新(update)操作异常.反之则是乱七八糟,不仅给 ...
- 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 ...
- 找工作笔试面试那些事儿(10)---SQL语句总结
SQL语句中常用关键词及其解释如下: 1)SELECT 将资料从数据库中的表格内选出,两个关键字:从 (FROM) 数据库中的表格内选出 (SELECT).语法为 SELECT "栏位名&q ...
- FLEX中Tree默认展开全部节点
这里分两种情况,一种是数据源在MXML文件里,如: <mx:XML id="treeXML" format="e4x"> <root> ...
- Linux文件 I/O 介绍
Linux文件 I/O 介绍 1. Linux系统调用 Linux系统调用(system call)是指操作系统提供给用户程序的一组"特殊接口",用户程序可以通过这组"特 ...