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表示是接下来要做的步骤,然后根据题意记 ...
随机推荐
- 6.11---上传图片遇到的bug,字节流输入流输出流----图解----图片必须是post
!!!这里要注意不能是目录必须是指定的文件名+目录,不然就存照片到指定的目录不成功 ----------------------------------------完整controller-servi ...
- 转 方法区(method) )、栈区(stack)和堆区(heap)之JVM 内存初学
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指 ...
- tomcat生成catalina.out文件
生成catalina.out方法 导语:本文为Windows下生tomcat将控制台信息输出到catalina.out文件 且 保证能实时查看日志文件的方法. 一.创建catalina.out 1. ...
- P1257 平面上的最接近点对
题目描述 给定平面上n个点,找出其中的一对点的距离,使得在这n个点的所有点对中,该距离为所有点对中最小的 输入输出格式 输入格式: 第一行:n:2≤n≤200000 接下来n行:每行两个实数:x y, ...
- 探索java世界中的日志奥秘
java日志简单介绍 对于一个应用程序来说日志记录是必不可少的一部分.线上问题追踪,基于日志的业务逻辑统计分析等都离不日志.JAVA领域存在多种日志框架,目前常用的日志 ...
- JS——正则案例
验证座机号码 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...
- 【sqli-labs】 less56 GET -Challenge -Union -14 queries allowed -Variation3 (GET型 挑战 联合查询 只允许14次查询 变化3)
单引号括号闭合 http://192.168.136.128/sqli-labs-master/Less-56/?id=1')%23 http://192.168.136.128/sqli-labs- ...
- tab切换案例
做个简单的tab切换效果,分别于jquery和js操作 (1)jQuery操作 先看下效果: <!DOCTYPE html> <html lang="en"> ...
- hdu 4870
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- select 如何将文本居中
开始测试了几种方式但是结果都是失败的,最后测试一种方式终于成功了,所以做下笔记: select{ width: 3.2rem; height: 1.2rem; border-radius: 0.6re ...