CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值。
析:很明显是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)的更多相关文章
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- 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 ...
- Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- 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 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- Codeforces 682C Alyona and the Tree
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
随机推荐
- 使用apache common-io 监控文件变化
package common.io; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.common ...
- fir 窗口设计法
加窗的原因.对于理想的低通滤波器H(exp(jw)),其h(n)是无限长序列.这是可以证明的.因此为了得到有限长的h(n)就需要截断,而这个过程就是加窗.由于h(n)截断即其频率响应就和理想的低通滤波 ...
- Java 排序(快排,归并)
Java 排序有Java.util.Arrays的sort方法,具体查看JDK API(一般都是用快排实现的,有的是用归并) package yxy; import java.util.Arrays; ...
- atl控件发布打包<转>
http://www.cnblogs.com/gengaixue/archive/2010/08/13/1799112.html ActiveX打包与发布 在VS2005或VS2008安装后发现路径* ...
- ZooKeeper 学习笔记(一)
第一章 ZooKeeper的基本概念 一.介绍 在过去,每个应用一般都是在单个机子(单处理器)上运行,现在这一状况已经发生了巨大的变化.在大数据和云计算的世界里,应用程序已经被分成多个独立的模块在不同 ...
- dart 公共变量
dart中可以直接在一个文件里声明一个变量,这在其他语言中并不常见,比如c#语言只有类型才可以在命名空间下定义,变量必须放在类里声明 所以dart这点特性类似于js 今天就来讨论这个公共变量的作用范围 ...
- AngularJS1.6版本中ui-router路由中/#!/的解决方法 - zhuan
本地编译出的文件可以正常运行,但是服务器编译后到了测试那里路由上就莫名的出现了/#!/,这导致了很多问题. 后来查了下是服务器编译器把AngularJS升级到了1.6版本,而我本地的依旧是1.5. 但 ...
- 运动函数封装(js)
// 运动函数 function starMove(obj,json,fnEnd){ clearInterval(obj.timer); obj.timer = setInterval(functi ...
- Superset安装
Superset version 1.8.5 # Install superset pip install cairocffi pip install superset yum ...
- laravel phpstorm IDE 代码提示
第一步:在项目的composer.json中添加如下一行 "require": { "laravel/framework": "5.0.*" ...