codeforces 682C C. Alyona and the Tree(dfs)
题目链接:
1 second
256 megabytes
standard input
standard output
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 v sad 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?
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.
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.
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
5 题意: 给一棵树,如果这个节点v与它的一个子节点v dist(u,v)>=a[u];那么这个节点就是sad节点,现在要你开始去掉叶子节点,问你最少去掉多少个节点才能使这棵树没有sad节点; 思路: dfs一发,dfs的时候一边找出这个节点到根的距离,一边维护这个节点到根节点路径上节点距离的最小值,因为dis[u]-min(dis[v])>a[u]就不满足了,这时dfs就可以return了;
如果满足就计算器加上这个点,继续dfs,最后的答案就是n-计数器的值; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+;
const int maxn=; int n,cnt=,num=,head[N];
LL a[N],dis[N];
struct Edge
{
int fr,to,va,next;
}edge[*N];
void addedge(int s,int e,int val)
{
edge[cnt].to=e;
edge[cnt].va=val;
edge[cnt].next=head[s];
head[s]=cnt++;
}
void dfs(int now,int fa,LL mmin)
{
if(dis[now]-mmin<=a[now])num++;
else return;
for(int i=head[now];i!=-;i=edge[i].next)
{
int fr=edge[i].to;
if(fr!=fa)
{
dis[fr]=dis[now]+edge[i].va;
dfs(fr,now,min(mmin,dis[now]));
}
}
} int main()
{
mst(head,-);
read(n);
Riep(n)read(a[i]);
int p,c;
Riep(n-)
{
read(p);
read(c);
addedge(i+,p,c);
addedge(p,i+,c);
}
dis[]=;
dfs(,-,inf);
printf("%d\n",n-num);
return ;
}
codeforces 682C C. Alyona and the Tree(dfs)的更多相关文章
- 【CodeForces - 682C】Alyona and the Tree(dfs)
Alyona and the Tree Descriptions 小灵决定节食,于是去森林里摘了些苹果.在那里,她意外地发现了一棵神奇的有根树,它的根在节点 1 上,每个节点和每条边上都有一个数字. ...
- 【Codeforces 682C】Alyona and the Tree
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设dis[v]表示v以上的点到达这个点的最大权值(肯定是它的祖先中的某个点到这个点) 类似于最大连续累加和 当往下走(x,y)这条边的时候,设 ...
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs
C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 381 D Alyona and a tree(倍增)(前缀数组)
Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 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 ...
随机推荐
- Selenium学习系列---- FirePath的安装和使用
在用Selenium编写测试用例的时候,需要对对网页元素上定位,而现在很多的浏览器是可以看到网页上相关的元素信息,可以查看某一个网页的元素信息,通过定位的方式查找元素.另外安装好Selenium ID ...
- 66. No EntityManager with actual transaction available for current thread【从零开始学】
[从零开始学习Spirng Boot-常见异常汇总] 具体异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: No En ...
- 【Ajax 3】JavaScript封装Ajax
导读:上一篇博客简单介绍了一下对Ajax中的核心对象XMLHttpRequest的封装.那么为了方便对Ajax的应用,我们还需要进一步的对Ajax的基本功能进行下一步的封装,不得不说的是早就有人专门写 ...
- [Kubernetes]集群配置免密登录Permission denied (publickey,password) 解决办法
在用ansible部署Kubernetes集群是需要配置免密登录,但是遇到Permission denied (publickey,password)的问题 首先推断可能是sshd_config的配置 ...
- 莫队乱搞--BZOJ2038: [2009国家集训队]小Z的袜子(hose)
$n \leq 50000$的$\leq 50000$的数字序列,$m \leq 50000$个询问,每次问一个区间中随机拿两次(不放回)拿到相同数字的概率,以既约分数形式输出. 莫队入门.把询问按“ ...
- mybatis连接mysql加密实现
参考文章1 参考文章2 直接重写BasicDataSource接口的setPassword方法: 这里同样存在一个问题,就是在重写的方法中,我们可以对password,username 进行解密处理, ...
- UITextInputMode currentInputMode is deprecated. 警告的解决
如果你的工程最低支持版本为7.0 你会发现有警告 : 'currentInputMode' is deprecated: first deprecated in iOS 7.0 替换方案:UIText ...
- SQL SERVER 2012 第五章 创建和修改数据表 の SQL SERVER中的对象名
[ServerName.[DataBaseName.[SchemeName.]]]ObjectName 服务器名,数据库名,模式名,对象名 其中模式是一个新出的坑爹的东西.
- Max Sum Plus Plus-HDU1024(dp)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- [Bzoj3631][JLOI2014]松鼠的新家 (树上前缀和)
3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2350 Solved: 1212[Submit][Sta ...