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

析:很明显是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. python学习 (三十) python的函数

     1: 函数参数默认值 def method1(p1 = , p2 = ): // 函数有两个参数,并且都有默认值 return p1 + p2 print(method1()) print(meth ...

  2. svg_png

    #!/usr/bin/env python#-*- encoding=UTF-8 -*-from __future__ import print_functionimport sysimport ra ...

  3. oracle的同义词总结

      oracle的同义词总结   从字面上理解就是别名的意思,和视图的功能类似.就是一种映射关系.   同义词拥有如下好处:   节省大量的数据库空间,对不同用户的操作同一张表没有多少差别;  扩展的 ...

  4. django -- url 的 默认值

    在urls.py里可以直接向函数传递默认值,看代码: urls.py from django.conf.urls import url from mytest import views urlpatt ...

  5. Mysql EXPLAIN列的解释

    转自:http://blog.chinaunix.net/uid-540802-id-3419311.html explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择 ...

  6. ios开发中view.layer.shouldRasterize = YES 的使用说明

    在做一个NavigationController push 子页面时,发现push和pop时很卡,研究了一大阵子后,发现在子页面里影响UI流畅的只有UIImageView的圆角设置:然后我就关闭了圆角 ...

  7. 【原】使用puppeteer爬虫下载Midi文件

    The Beatles 乐队的 Midi文件下载地址 puppeteer官方github地址 midi文件爬取示例代码github地址 1.安装npm 参考:安装npm及cnpm(Windows) 修 ...

  8. 解决linux下root运行Elasticsearch异常

    如果以root身份运行将会出现以下问题 root@yxjay:/opt/elasticsearch-2.3.5/bin# ./elasticsearchException in thread &quo ...

  9. centos7 安装wxPython

    *** exact error that occured. This usually means GTK+ is incorrectly installed.    configure: error: ...

  10. Hibernate批量抓取

    ------------------siwuxie095 Hibernate 批量抓取 以客户和联系人为例(一对多) 1.批量抓取 同时查询多个对象的关联对象,是 Hibernate 抓取策略的一种 ...