CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree
题目链接:
http://acm.hust.edu.cn/vjudge/contest/121333#problem/C
Description
Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly found a magic rooted tree with root in the vertex 1, every vertex and every edge of which has a number written on.
The girl noticed that some of the tree's vertices are sad, so she decided to play with them. Let's call vertex vsad if there is a vertex u in subtree of vertex v such that dist(v, u) > au, where au is the number written on vertex u, dist(v, u) is the sum of the numbers written on the edges on the path from v to u.
Leaves of a tree are vertices connected to a single vertex by a single edge, but the root of a tree is a leaf if and only if the tree consists of a single vertex — root.
Thus Alyona decided to remove some of tree leaves until there will be no any sad vertex left in the tree. What is the minimum number of leaves Alyona needs to remove?
Input
In the first line of the input integer n (1 ≤ n ≤ 105) is given — the number of vertices in the tree.
In the second line the sequence of n integers a1, a2, ..., an (1 ≤ ai ≤ 109) is given, where ai is the number written on vertex i.
The next n - 1 lines describe tree edges: ith of them consists of two integers pi and ci(1 ≤ pi ≤ n, - 109 ≤ ci ≤ 109), meaning that there is an edge connecting vertices i + 1 and pi with number ci written on it.
Output
Print the only integer — the minimum number of leaves Alyona needs to remove such that there will be no any sad vertex left in the tree.
Sample Input
Input
9
88 22 83 14 95 91 98 53 11
3 24
7 -8
1 67
1 64
9 65
5 12
6 -80
3 8
Output
5
题意:
删除最少的叶节点,使得不存在祖先路径之和大于点权的点;
题解:
直接dfs判断每个点是否要被删除,找出所有不需要删除的点;
WA:路径权和为负时应置零----因为判断的是所有祖先点到该点的路径.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 150000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
typedef pair<int,LL> pii;
vector<pii> g[maxn];
int n;
LL num[maxn];
bool vis[maxn];
int ans;
void dfs(int u, LL sum) {
vis[u] = 1;
if(sum <= num[u]) ans++;
else return;
int sz = g[u].size();
for(int i=0; i<sz; i++) {
if(vis[g[u][i].first]) continue;
dfs(g[u][i].first, max(0LL,sum+g[u][i].second));
}
}
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d",&n) != EOF)
{
memset(vis, 0, sizeof(vis));
for(int i=1; i<=n; i++) g[i].clear();
for(int i=1; i<=n; i++) scanf("%I64d", &num[i]);
for(int i=1; i<=n-1; i++) {
int v;LL w; scanf("%d %I64d",&v,&w);
g[v].push_back(make_pair(i+1,w));
g[i+1].push_back(make_pair(v,w));
}
ans = 0LL;
dfs(1, 0LL);
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和其整个子树都将被删去,求被 ...
- 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+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- Codeforces 682C Alyona and the Tree
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- css控制UL LI 的样式详解
用<ul>设置导航 <style> #menu ul {list-style:none;margin:0px;} #menu ul li {float:left;} </ ...
- Java Swing中Substance常用皮肤
AutumnSkin; BusinessSkin; BusinessBlackSteelSkin; BusinessBlueSteelSkin; ChallengerDeepSkin; CremeSk ...
- BZOJ 1000: A+B Problem
问题:A + B问题 描述:http://acm.wust.edu.cn/problem.php?id=1000&soj=0 代码示例: import java.util.Scanner; p ...
- sdut 2934 人活着系列之平方数 (完全背包变形)
题目链接 分析:完全背包的变形,每一层的d[]数组代表这一层的这个数新加入以后所构成的val的种类. #include <iostream> #include <cstdio> ...
- 禁止ie缓存
nocache.jsp:(后台配置)<%response.setHeader("Cache-Control","no-cache"); //HTTP 1. ...
- centos的版本和内核查看
查看linu的内核信息 查看distrubution,centos属于哪个release
- bzoj3955
首先,最短路不同的两辆车一定不会发生堵塞 对于最短路相同的点,我们把属于最短路径上的边拎出来建图跑最大流即可 然后我TLE了…… 因为很明显建出来图很大,而真正流的流量很小 普通的初始标号都是0的sa ...
- 终极解决方案:windows10资源管理器假死
想解决这个问题有以下几点: 1,不要相信所谓的powershell里面输命令可以重装系统自带应用从而解决假死问题! 2,不要相信烂大街的“自启动User Manager”服务可以解决假死问题! 3,不 ...
- mssql修改链接数为默认值
EXEC sys.sp_configure N'show advanced options', N'1' RECONFIGURE WITH OVERRIDE GO EXEC sys.sp_confi ...
- HDU 1397 Goldbach's Conjecture【素数打表】
题意:给出n,问满足a+b=n且a,b都为素数的有多少对 将素数打表,再枚举 #include<iostream> #include<cstdio> #include<c ...