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/D
Description
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).
Input
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.
Output
Print the number of valid sets modulo 1000000007.
Sample Input
1 4
2 1 3 2
1 2
1 3
3 4
Sample Output
8
HINT
题意
给你一颗树,然后让你找到里面有多少棵子树,满足子树里面最大点减去最小点的差小于等于d
题解:
直接枚举每一个点为根,然后开始dfs,都假设这个根节点是这棵子树的最大值
然后乘法原理搞一搞就好啦
代码
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define maxn 2005
const int mod = 1e9 + ;
int a[maxn];
int d,n;
vector<int> G[maxn];
long long dfs(int x,int pre,int k)
{
long long ans = ;
for(int i=;i<G[x].size();i++)
{
int v = G[x][i];
if(v == pre || (a[k] == a[v]&&v > k))continue;
if(a[k]>=a[v]&&a[k]-a[v]<=d)
{
ans *= ( dfs(v,x,k) + );
while(ans>=mod)ans%=mod;
}
}
return ans;
}
int main()
{
scanf("%d%d",&d,&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
{
int u,v;scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
long long ans = ;
for(int i=;i<=n;i++)
{
ans += dfs(i,-,i);
while(ans>=mod)ans%=mod;
}
printf("%lld\n",ans);
}
Codeforces Round #277 (Div. 2) D. Valid Sets 暴力的更多相关文章
- 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 Round #268 (Div. 1) B. Two Sets 暴力
B. Two Sets Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/468/problem/B ...
- 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 #277 (Div. 2)D(树形DP计数类)
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- Android中ListView嵌套进ScrollView时高度很小的解决方案
package com.example.test.util; import android.view.View; import android.view.ViewGroup; import andro ...
- java 中的Exception RuntimeException 区别
在java的异常类体系中: 1.Error和RuntimeException是非检查型异常,其他的都是检查型异常; 2.所有方法都可以在不声明throws的情况下抛出RuntimeException及 ...
- UML类图设计
大纲: 在Visio里,包和类的关系是包含关系,将类拖入包的文件夹之后,关系就建立了,二元关联符号可以设置为:聚合.合成.接口:空心圆+直线(唐老鸭类实现了‘讲人话’):依赖:虚线+箭头(动物和空气的 ...
- FOJ 1858 Super Girl 单调队列
http://acm.fzu.edu.cn/problem.php?pid=1858 一个数组中 找两对元素,第一对元素和最大,第二对元素和最小,限制:一对元素中两个元素的距离在原数组中小于d.去掉 ...
- <转>亲手缔造DNS体系,创建DNS私有根:DNS系列之六
打造DNS私有根 我们现在已经从前面的博文中了解到了很多DNS的相关知识,今天我们用一个综合性的实验把前面的内容都串起来复习一下,这个有趣的实验就是DNS的私有根.私有根顾名思义是由个人或企业自行创建 ...
- cocos2d-x 不能在android真机debug的问题
最近在做cocos2d-x开发的时候,发现在android真机上不能调试C++代码,显示如下警告信息 Ignoring packet error, continuing... warning: unr ...
- 利用redis分布式锁的功能来实现定时器的分布式
文章来源于我的 iteye blog http://ak478288.iteye.com/blog/1898190 以前为部门内部开发过一个定时器程序,这个定时器很简单,就是配置quartz,来实现定 ...
- UML 学习
推荐书籍:<面向对象分析与设计(第3版)>.<UML精粹:标准对象建模语言简明指南(第3版)> 推荐一: http://amateras.sourceforge.jp/cgi- ...
- pku3668 Game of Lines
http://poj.org/problem?id=3668 水题,STL #include <stdio.h> #include <set> using namespace ...
- 新版本ffmpeg解码非完整H264帧失败
按照ffmpeg/doc/examples/decoding_encoding.c中video_decode_example解码H264,新版本ffmpeg解码非完整H264帧,定量读取数据直接给av ...