暴力——BZOJ2783/Luogu3252 [JLOI2012]树
来源:互联网 发布:淘宝注册商家有多少家 编辑:程序博客网 时间:2024/06/05 07:50
题面:BZOJ2783 Luogu3252
淡定地用dfs+前缀和直接水过这题
前缀和查找直接沿着树高找上去好了
是不是很暴力呢?
#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<iostream>#include<cstdlib>#include<queue>#include<string>#include<ctime>#include<climits>using namespace std;bool rt[100001];int n,m,nedge=0,p[100001],nex[100001],head[100001],fa[100001],s[100001],a[100001],ans=0;inline void addedge(int a,int b){p[++nedge]=b;nex[nedge]=head[a];head[a]=nedge;}inline void dfs(int x){ s[x]=s[fa[x]]+a[x]; for(int rp=fa[x];rp>-1;rp=fa[rp]){ if(s[x]-s[rp]==m)ans++; if(s[x]-s[rp]>=m)break; } for(int k=head[x];k;k=nex[k])dfs(p[k]);}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<n;i++){ int x,y;scanf("%d%d",&x,&y);addedge(x,y); fa[y]=x;rt[y]=1; } fa[0]=-1;s[0]=0; for(int i=1;i<=n;i++)if(!rt[i]){ fa[i]=0;dfs(i); } printf("%d",ans); return 0;}
阅读全文
1 0
- 暴力——BZOJ2783/Luogu3252 [JLOI2012]树
- 【bzoj2783】【JLOI2012】【树】【set】
- 【JLOI2012】【BZOJ2783】树
- bzoj2783【JLOI2012】树
- bzoj2783 JLOI2012 树
- bzoj2783: [JLOI2012]树
- 【bzoj2783】【JLOI2012】树
- bzoj2783 [JLOI2012]树 STL+dfs
- 【BZOJ2783】【DFS】[JLOI2012]树 题解
- 2783: [JLOI2012]树
- 2783: [JLOI2012]树 set
- BZOJ 2783 [JLOI2012]树
- 【BZOJ 2783】 [JLOI2012]树
- BZOJ 2783: [JLOI2012]树
- 2783: [JLOI2012]树
- 洛谷P3252 [JLOI2012]树
- bzoj 2783: [JLOI2012]树
- BZOJ 2783: [JLOI2012]树
- 设计模式学习笔记——简单工厂模式、工厂方法模式、抽象工厂模式
- 双向循环链表
- cocos 寻路 lua实现
- 一个关于数据库迁移的小错误
- Altera Modelsim仿真sim模板
- 暴力——BZOJ2783/Luogu3252 [JLOI2012]树
- 使用FFmpeg实现抠图合并功能(chroma key)
- C中struct类型以及指针链表
- Hibernate的HQL简介
- 单链表
- Java 8 Streams map() 例子
- 我是新手
- 值不能为空。参数名viewinfo(microsoft.sqlserver.management.sqlstudio.explorer)
- vue中$nextTick()作用