codeforces 682C Alyona and the Tree DFS
2016-06-25 08:33
525 查看
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值
View Code
#include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <stack> #include <queue> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const int N=1e5+5; const int INF=0x3f3f3f3f; const int mod=1e9+7; struct Edge{ int v,w,next; }edge[N<<1]; int head ,tot,a ,n; bool del ; void add(int u,int v,int w){ edge[tot].v=v; edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot++; } int ret; void dfs(int u,int f,LL cur,LL mn){ if(del[f])del[u]=true,++ret; else if(cur-mn>a[u])del[u]=true,++ret; for(int i=head[u];~i;i=edge[i].next){ int v=edge[i].v; if(v==f)continue; dfs(v,u,cur+1ll*edge[i].w,min(cur+1ll*edge[i].w,mn)); } } int main(){ scanf("%d",&n); for(int i=1;i<=n;++i)scanf("%d",&a[i]); memset(head,-1,sizeof(head)); for(int i=2;i<=n;++i){ int u=i,v,w; scanf("%d%d",&v,&w); add(u,v,w);add(v,u,w); } dfs(1,0,0,0); printf("%d\n",ret); return 0; }
View Code
相关文章推荐
- 关于TCP传输速率的测量方法
- 灰色图片
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
- Java面试题和答案——(下)
- django 接受 ajax 传来的数组对象
- 码农的联系方式
- sql题
- 【GIT-3】GIT 的基础教程 分支管理 fixbug分支 Featur 冲突处理 标签管理 操作
- 最短路径问题
- JSP用户登录页面
- 每天一个Linux命令(58)sudo命令
- 检测字符串 字母数字中文
- Android 批量添加联系人
- 看 nova-scheduler 如何选择计算节点 - 每天5分钟玩转 OpenStack(27)
- sublime快捷键-for mac
- 开源Java EE框架 JBoss Seam 详细介绍
- python学习(2)
- 基于Hadoop生态圈的数据仓库实践 —— 环境搭建(一)
- Leetcode 230. Kth Smallest Element in a BST
- 水晶报表打印出错,未能加载文件或程序集“CrystalDecisions.CrystalReports.Engine, Version=10.5.3700.0