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 ...
随机推荐
- Oracle 课程七之分析和动态采样
课程目标 完成本课程的学习后,您应该能够: •引子—统计信息的作用 •如何收集统计信息 •系统统计信息 •对象统计信息—表.字段.索引统计信息 •动态采样 统计信息的作用 Optimizer st ...
- xp重装系统后恢复Linux启动
我的电脑----右键-----属性----高级----启动与恢复故障----设置编辑 [boot loader]timeout=3default=multi(0)disk(0)rdisk(0)part ...
- Android主题theme和风格style总结
用到了Android的主题和风格,感觉很多地方需要总结和记录下来.其实主题和风格是有很大的作用的,特别是界面要求比较高的客户端. Style:是一个包含一种或者多种格式化属性的集合,我们可以将其用为一 ...
- 【Windows核心编程】重载类成员函数new / new[] / delete / delete[]
// Heap.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...
- Mahout分步式程序开发 聚类Kmeans(转)
Posted: Oct 14, 2013 Tags: clusterHadoopkmeansMahoutR聚类 Comments: 13 Comments Mahout分步式程序开发 聚类Kmeans ...
- oc_转_NSInteger 和 NSNumber
Objective-C 支持的类型有两种:基本类型和类. 基本类型,如同 C 语言中的 int 类型一样,拿来就可以直接用.而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值.类的 ...
- BootStrap入门教程 (一) :手脚架Scaffolding(全局样式(Global Style),格网系统(Grid System),流式格网(Fluid grid System),自定义(Customing),布局(Layouts))
2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MARK ...
- 为Hadoop配置Win8.1授时服务器
启动Windows服务,顺序如下: SSTPS(secure socket tunneling protocol service)服务 Telephony服务 Remote Access Connec ...
- 《Java数据结构与算法》笔记-CH3简单排序
class ArrayBub { private long[] arr; private int nElement; public ArrayBub(int size) { arr = new lon ...
- openstack instance snapshort
从下面的截图 高手应该能够看到openstack基于snapshort的 实例的 备份策略了!!!! 从下面某张截图你也能看到用openstack snapshort备份 也是 不太理想的,首先从“磁 ...