题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值。

析:很明显是DFS,不过要想找出最少的结点可能不太容易,所以我们可以先找出剩下结点最多,那么用总数减去这个就好,那么怎么找哪些结点是剩下的呢?首先要知道,如果一个结点要被删掉,

那么它的子树肯定也要被删掉,并且,要满足dist(v, u) <= a[u]才是可能留下的,那么只要dist(v, u) >a[u],就不要,所以一定要注意的是,结点的距离可能为负,所以要在DFS时判断一下,肯定是取非负的,

想一想为什么。

代码如下:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 5;
typedef long long LL;
typedef pair<int, int> P;
int a[maxn];
vector<P> G[maxn];
int ans; void dfs(int u, LL d, int fa){
if(a[u] < d) return ;//不满足要删掉
++ans;//计算最多有多少结点
for(int i = 0; i < G[u].size(); ++i)
if(G[u][i].first == fa) continue;//不能返回父结点
else dfs(G[u][i].first, max(0LL, d+G[u][i].second), u);//判断是不是大于等于0
} int main(){
int n, p, c;
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i < n; ++i){
cin >> p >> c;
G[i+1].push_back(P(p, c));
G[p].push_back(P(i+1, c));
}
ans = 0;
dfs(1, 0, -1);
printf("%d\n", n - ans);
return 0;
}

CodeForces 682C Alyona and the Tree (树上DFS)的更多相关文章

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

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

  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. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  4. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...

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

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

  6. 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 ...

  7. codeforces 682C Alyona and the Tree DFS

    这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...

  8. Codeforces 682C Alyona and the Tree

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

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

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

随机推荐

  1. 在Centos中导入sql文件的方法

    在Centos中导入sql文件的方法 利用mysql -u root -p进入mysql数据库 对于文件的导入,在Centos下里面的是首先要新建一个和文件相同名字的数据库.例如:mysql>c ...

  2. opatch lsinventory –details

    今天把RAC的数据库升完级后,在RAC1节点执行opatch lsinventory –detail 命令,没有报错.在rac2节点执行报错: [oracle@rac2 ~]$ opatch lsin ...

  3. solr解决访问安全

    Tomcat7,solr3.6,mmseg1.8 1:环境的搭建 1:解压tomcat,solr,mmseg4j 2:复制dist文件夹下apache-solr.war到tomcat的webapp文件 ...

  4. 04——wepy框架搭建

    wepy官方文档:https://tencent.github.io/wepy/document.html#/ 1.项目下载 # .安装wepy-cli npm install wepy-cli -g ...

  5. touch python

    一  使用while循环输出 1 2 3 4 5 6 8 9 10. i=0 while i<10: i=i+1 if i == 7: continue print(i) 二  求 1-100所 ...

  6. Linux基础综合练习

    Linux基本操作综合练习 1.建立用户zhangsan,密码使用明文123456: 命令:useradd -p 123456 zhangsan 解释: 参数 -p 添加明文密码 useradd添加用 ...

  7. Disconf实践指南:使用篇

    在上一篇文章Disconf实践指南:安装篇介绍了如何在本地搭建Disconf环境,下面我们介绍如何在项目中使用Disconf.由于某些功能特性对源码做了修改,所以在官方文档并没有提及. 环境基于mac ...

  8. Quest *nix Xwindows

    国内一般网站搜到的linux系统添加Xwindows都是无法完成的,至少我在Ct6.3上不行,也许是yum源配置不同问题 我举个站点大家自己上去看,本人就不多说了,多说也无益,. http://yao ...

  9. Java--神奇的hashcode

    一.Object的HashCode定义 public native int hashCode(); Object类的hashCode方式使用了native修饰也就意味着真正的实现调用的其他语言编写的方 ...

  10. JS比较两个数组是否相等 是否拥有相同元素

    Javascript怎么比较两个数组是否相同?JS怎么比较两个数组是否有完全相同的元素?Javascript不能直接用==或者===来判断两个数组是否相等,无论是相等还是全等都不行,以下两行JS代码都 ...