Codeforces Round #277 (Div. 2) D. Valid Sets DP
As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it.
We call a set S of tree nodes valid if following conditions are satisfied:
- S is non-empty.
- S is connected. In other words, if nodes u and v are in S, then all nodes lying on the simple path between u and v should also be presented in S.
- .
Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007(109 + 7).
The first line contains two space-separated integers d (0 ≤ d ≤ 2000) and n (1 ≤ n ≤ 2000).
The second line contains n space-separated positive integers a1, a2, ..., an(1 ≤ ai ≤ 2000).
Then the next n - 1 line each contain pair of integers u and v (1 ≤ u, v ≤ n) denoting that there is an edge between u and v. It is guaranteed that these edges form a tree.
Print the number of valid sets modulo 1000000007.
1 4
2 1 3 2
1 2
1 3
3 4
8
In the first sample, there are exactly 8 valid sets: {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {3, 4} and {1, 3, 4}. Set {1, 2, 3, 4} is not valid, because the third condition isn't satisfied. Set {1, 4} satisfies the third condition, but conflicts with the second condition.
题意:给你一个n点的树,和每个点的权值,问你多少种子树满足(最大权值点-最小权值点)<=d
题解:定义dp[i]表示以i为最小权值根节点的子树方案数,注意维护此条件
于是答案就是 ∑dp[i] %mod (1<=i<=n);
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define meminf(a) memset(a,127,sizeof(a)); inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
#define maxn 2000+50
#define mod 1000000007
#define inf 1000000007
int d,n,a[maxn],vis[maxn];
vector<int >G[maxn];
ll dp[maxn];//以i为最小根节点,的方案数
void dfs(int x,int pre){
dp[x]=;vis[x]=;
for(int i=;i<G[x].size();i++){
if(!vis[G[x][i]]){
if(a[G[x][i]]<a[pre]||a[G[x][i]]>a[pre]+d)continue;
if(a[G[x][i]]==a[pre]&&G[x][i]<pre)continue;
dfs(G[x][i],pre);
dp[x]=(dp[x]*(dp[G[x][i]]+))%mod;
}
}
} int main(){
d=read(),n=read();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}int u,v;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
G[u].pb(v);G[v].pb(u);
}ll ans=;
for(int i=;i<=n;i++){
mem(dp);mem(vis);
dfs(i,i);
ans=(ans+dp[i])%mod;
}
cout<<ans<<endl;
return ;
}
代码
Codeforces Round #277 (Div. 2) D. Valid Sets DP的更多相关文章
- 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 暴力
D. Valid Sets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/problem ...
- Codeforces Round #277 (Div. 2)D(树形DP计数类)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
- 套题 Codeforces Round #277 (Div. 2)
A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...
- Codeforces Round #277(Div 2) A、B、C、D、E题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. Calculating Function 水题,判个奇偶即可 #includ ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
随机推荐
- 11.Layers, Containers and Interfaces
图层.容器和接口 当设计一个Ventuz场景时,某些节点的组合或设计会反复出现.例如在演示中使用的按钮或滑块,在整个过程中的呈现和外观都是一致的,唯一变化的是尺寸.位置和标签. 在设计复杂的演示时,另 ...
- Laravel 5.4.36 session 发现
由于Laravel session机制完全脱离了PHP自带的session机制 因此对于php.ini 配置session对Laravel 是不会产生影响 代码路径: vendor/larav ...
- Android开发高手课 - 02 崩溃优化(下):应用崩溃了,你应该如何去分析?
崩溃现场 1. 崩溃信息 进程名.线程名 崩溃类型和堆栈信息 2. 系统信息 Logcat 机型.系统.厂商.CPU.ABI.Linux 版本等 设备状态:是否 root.是否模拟器.是否有 Xpos ...
- jQuery——表格添加数据
1.遮罩层宽高100%,position,不占位 2.注册a标签的删除事件,用on()方法,以方法可以动态添加,之前js需要利用冒泡属性(父标签注册事件,子标签冒泡,target===li触发事件) ...
- Python_多线程1(创建线程,简单线程同步)
threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...
- Mock随机生成数据模拟后台接口
<html> <head> <title>测试</title> <script src="http://code.jquery.com/ ...
- centos 7 配置nginx
安装nginx: curl -o nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0. ...
- Mysql命令mysql:连接Mysql数据库
mysql命令格式: mysql -h主机地址 -u用户名 -p用户密码 1) 连接到本机上的MYSQL首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p, ...
- Nginx +tomcat 实现负载均衡集群
一. 工具 nginx-1.8.0 apache-tomcat-6.0.33 二. 目标 实现高性能负载均衡的Tomcat集群: 三. 步骤 1.首先下载Nginx ...
- 【GC】
using 语句适用于清理单个非托管资源的情况,而多个非托管对象的清理最好以 try-finnaly 来实现,因为嵌套的 using 语句可能存在隐藏的 Bug.内层 using 块引发异常时,将不能 ...