题目链接:http://codeforces.com/problemset/problem/682/C

题目大意:
取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么称节点v是伤心的。
给你一个根为1的树,每个节点有一个权值a[i],每条边也有一个权值w,
现在让你删最少的结点,使得树上不存在伤心的点。
解题思路:
删除最少的点,我们可以反一下,变成找最多的点,使得这些点不伤心。
只要对这棵树进行DFS,同时记录路径长度dis,当到达某点u时,
若dis>a[u],那么要将u及u的子树删除,所以直接return,不再继续遍历。
注意,这里的dis是1~u的最长路径,所以当dis<0时使得dis=0,类似最大子段和的做法。

代码:

 #include<bits/stdc++.h>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=2e5+;
const int INF=0x3f3f3f3f;
const double eps=1e-; struct node{
int to,w;
node(int to,int w):to(to),w(w){}
}; int ans;
int a[N];
vector<node>v[N]; void dfs(int u,LL dis,int fa){
if(dis>a[u]) return; //dis(v,u)>a[u]不符合
ans++;
for(int i=;i<v[u].size();i++){
node t=v[u][i];
if(t.to==fa) continue;
dfs(t.to,max(0LL,dis+t.w),u);//dis<0则取0
}
} int main(){
FAST_IO;
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>a[i];
}
for(int i=;i<=n;i++){
int to,w;
cin>>to>>w;
v[i].push_back(node(to,w));
v[to].push_back(node(i,w));
}
dfs(,,-);
cout<<n-ans<<endl;
return ;
}

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

  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(树形DP)

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

  3. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

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

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

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

  6. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

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

  8. codeforces 682C Alyona and the Tree DFS

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

  9. Codeforces 682C Alyona and the Tree

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

随机推荐

  1. No module named 'urllib.request'; 'urllib' is not a package

    想学爬虫urllib的设置代理服务器,于是把之前跳过没学的urllib捡起来,敲了段简单的代码,如下 import urllib.request url = "http://www.baid ...

  2. Linux /etc/issue 和 /etc/issue.net的作用和区别

    1./etc/motd /etc/motd即messageoftoday(布告栏信息),每次用户登录时,/etc/motd文件的内容会显示在用户的终端.系统管理员可以在文件中编辑系统活动消息,例如:管 ...

  3. asp.net webapi http请求生命周期

    先附上webapi http生命周期图. 原始的图片地址为:https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf

  4. CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries

    https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...

  5. Redis学习二:Redis入门介绍

    一.入门概述 1.是什么 Redis:REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value)分布式内 ...

  6. Arcgis10.1 Arcobject连接Oracel数据库

    原来使用Arcgis9.3的版本,现在升级到了10.1遇到不少问题,原来初始化工作空间的代码无法正常运行了,修改后的代码如下: static void Test() { IPropertySet sd ...

  7. 20155307 2016-2017-2 《Java程序设计》第6周学习总结

    20155307 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 串流数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出,可以使用输入串流:如果 ...

  8. 使用spring的监听器来完成系统超级管理员的注册

    1.注入 2“util类 package com.liveyc.mgrsite.util; import org.springframework.beans.factory.annotation.Au ...

  9. post请求远程url 报错“基础连接已经关闭...Authentication.AuthenticationException...远程证书无效”解决方案

    当我们有时用代码编写post请求url远程地址会报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系. ---> System.Security.Authentication.A ...

  10. Groovy/Spock 测试导论

    Groovy/Spock 测试导论 原文 http://java.dzone.com/articles/intro-so-groovyspock-testing 翻译 hxfirefox 测试对于软件 ...