Codeforces 486D D. Valid Sets
http://codeforces.com/contest/486/problem/D
题意:给定一棵树,点上有权值,以及d,要求有多少种联通块满足最大值减最小值小于等于d。
思路:枚举i作为最大的点权,然后dfs树规一下,就能得出以这个点为最大值的方案数,因为有权值相等的点,所以我们规定一下,只能从标号小的拓展到标号大的,就不会重复了。
1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 #define ll long long 7 const ll Mod=1000000007; 8 int tot,go[200005],first[200005],next[200005],a[200005],d,n; 9 ll f[200005]; 10 int read(){ 11 int t=0,f=1;char ch=getchar(); 12 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 13 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 14 return t*f; 15 } 16 void insert(int x,int y){ 17 tot++; 18 go[tot]=y; 19 next[tot]=first[x]; 20 first[x]=tot; 21 } 22 void add(int x,int y){ 23 insert(x,y);insert(y,x); 24 } 25 void dfs(int x,int fa,int fi){ 26 f[x]=1; 27 for (int i=first[x];i;i=next[i]){ 28 int pur=go[i]; 29 if (pur==fa) continue; 30 if (a[pur]>a[fi]) continue; 31 if (a[fi]-d>a[pur]) continue; 32 if (a[fi]==a[pur]&&fi>pur) continue; 33 dfs(pur,x,fi); 34 f[x]*=(1LL+f[pur]); 35 f[x]%=Mod; 36 } 37 } 38 int main(){ 39 d=read();n=read(); 40 for (int i=1;i<=n;i++) 41 a[i]=read(); 42 for (int i=1;i<n;i++){ 43 int x=read(),y=read(); 44 add(x,y); 45 } 46 ll ans=0; 47 for (int i=1;i<=n;i++){ 48 for (int j=1;j<=n;j++) f[j]=0; 49 dfs(i,0,i); 50 ans=(ans+f[i])%Mod; 51 } 52 printf("%I64d\n",ans); 53 }
转载于:https://www.cnblogs.com/qzqzgfy/p/5608635.html
Codeforces 486D D. Valid Sets相关推荐
- Codeforces 486D. Valid Sets
D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces B. Eight Point Sets 解题报告
题目链接:http://codeforces.com/problemset/problem/334/B 一开始看到题目,有点怯,理解了题目后,其实并不难.这句话是突破口 three distinct ...
- 【codeforces 103E】 Buying Sets
http://codeforces.com/problemset/problem/103/E (题目链接) 题意 给出$n$个数,每个数与一个集合相关联.从其中选出最小的若干个数,选出的数的个数与这些 ...
- 【CodeForces - 722D】Generating Sets(二分,贪心)
题干: You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct posit ...
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- Codeforces Round #277(Div 2) A、B、C、D、E题解
转载请注明出处: http://www.cnblogs.com/fraud/ --by fraud A. Calculating Function 水题,判个奇偶即可 1 #inc ...
- C++ Tricks
以下内容大部分使用 C++11 ! 以下内容大部分使用 C++11 ! 以下内容大部分使用 C++11 ! 通过 {} 来给容器赋值 pair<int, int> p; // ... p ...
- 出现字迹模糊迹象_改变迹象:如何使用动态编程解决竞争性编程问题
出现字迹模糊迹象 by Sachin Malhotra 由Sachin Malhotra 改变迹象:如何使用动态编程解决竞争性编程问题 (Change the signs: how to use dy ...
- Pytorch笔记-6
Text Classification with the torchtext library 本教程,我们将展示如何使用torchtext库,构建文本分类分析用的数据集.用户将能灵活做以下几项: Ac ...
最新文章
- 在RedHat4 64位操作系统下,安装Oracle 10g
- centos下排查vsftpd出现put零字节问题的记录
- 读取带空格字符串小结
- vmware 5.51 下安装Ubuntu Linux8.04安装vmware tools不成功
- 惠普:存储为未来而设计
- Hibernate的延迟加载问题
- tomcat知识点总结
- 老顽童java模拟器_在这款神还原的小霸王模拟器上,我终于玩到了20年前的老游戏,真香...
- php留言板制作模板,简单5步,制作wordpress留言板
- 华为OSN1500B故障应急处理
- Kvaser Memorator数据记录仪通过KVmlib配置和读取SD卡
- vue 刮刮乐抽奖H5项目
- 一台电脑绿色安装多个版本google Chorme方案
- Excel中插入图表后在设计选项卡无法选择样式解决办法
- Codeforces1183 E. Subsequences (easy version)(bfs)
- 写作人的福音——obsidian非官方插件之senGener
- 影视网站导航PHP源码
- 批处理系统脱机使用计算机,1.2.2 脱机输入/输出和批处理系统
- 2020年Java面试题及答案_Java面试宝典_Java笔试题(持续更新中)
- 现如今软件测试还有前景吗?