这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值

#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+;
const int INF=0x3f3f3f3f;
const int mod=1e9+;
struct Edge{
int v,w,next;
}edge[N<<];
int head[N],tot,a[N],n;
bool del[N];
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=;i<=n;++i)scanf("%d",&a[i]);
memset(head,-,sizeof(head));
for(int i=;i<=n;++i){
int u=i,v,w;
scanf("%d%d",&v,&w);
add(u,v,w);add(v,u,w);
}
dfs(,,,);
printf("%d\n",ret);
return ;
}

codeforces 682C Alyona and the Tree DFS的更多相关文章

  1. XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...

  2. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  3. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  4. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  5. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  6. Codeforces 682C Alyona and the Tree

    题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...

  7. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  8. CodeForces 682C Alyona and the Tree(广搜 + 技巧)

    方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...

  9. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. MySQL 当记录不存在时插入(insert if not exists)

    在 MySQL 中,插入(insert)一条记录很简单,但是一些特殊应用,在插入记录前,需要检查这条记录是否已经存在,只有当记录不存在时才执行插入操作,本文介绍的就是这个问题的解决方案.问题:我创建了 ...

  2. 【memcache缓存专题(1)】memcache的介绍与应用场景

    简介 Memcached是一个高性能的分布式的内存对象缓存系统,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各 ...

  3. windows环境下安装 zookeeper

    我们下载下来的zookeeper的安装包是.tar.gz格式的,但是还是可以在windows下运行. 下载地址 http://mirrors.hust.edu.cn/apache/zookeeper/ ...

  4. spring boot 1.4默认使用 hibernate validator

    spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能.hibernate validator 5.2.4 Final是JSR 349 Be ...

  5. MyEclipse 常用设置

    1.指定本机java环境 Windows-->preferences-->java-->Insetallel JREs 右侧 单击ADD-->standard VM--> ...

  6. 【POJ】2170 Lattice Animals

    1. 题目描述给定$n \times m, n.m \in [1, 10]$的方格,求不同形状的$[1 \cdots 10]$联通块的个数?所谓不同形状,表示不能通过平移.旋转.镜像实现相同的形状.2 ...

  7. ios程序开发杂记

    ios程序开发杂记 一.程序构建 与一般的程序构建无太大区别,都是源文件编译链接这一套,通常是在mac上做交叉编译,也就是利用xcode里带的ios编译工具集去生成arm架构的ios程序(或是x86的 ...

  8. HDU 4946 共线凸包

    题目大意: 一些点在一张无穷图上面,每个点可以控制一些区域,这个区域满足这个点到达这个区域的时间严格小于其他点.求哪些点能够控制无穷面积的区域. 题目思路: 速度小的控制范围一定有限. 速度最大当且仅 ...

  9. open_table

    /* Open a table. SYNOPSIS open_table() thd Thread context. table_list Open first table in list. acti ...

  10. bzoj3926

    题目的意思是叶子不超过20个……听说当初zjoi不少人被坑 分别对每个叶子以它为根dfs出20个dfs树,这样整个树的任何一个子串,都是某个dfs树上一个点到它的一个子孙的路径 每个dfs树,根到叶子 ...