Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接: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)的更多相关文章
- 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(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于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)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- 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 ...
随机推荐
- python之旅:python中range()和len()函数区别
函数:len() 作用:返回字符串.列表.字典.元组等长度 语法:len(str) 参数: str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度 实例 1.计算字符 ...
- pg数据库杀掉连接
遇到异常连接时,需要将对应连接杀掉,可能是连接占用了过多CPU或是IO,影响了业务,或是时间过长的空闲事务. pg对于杀掉连接提供了专门的命令,一般情况下使用pg_cancel_backend就可以, ...
- laravel mapSpread 例子
$collection = collect(range(1, 9)); $chunks = $collection->chunk(2); $labeld = $chunks->mapSpr ...
- Chapter12(动态内存)--C++Prime笔记
1.分配再静态或栈内存中的对象由编译器自动创建销毁. 2.C++中动态内存的管理是通过 new:前者为对象非配空间并返回一个指向该对象的指针. delete:接受一个动态对象的指针,摧毁该对象,并释放 ...
- supervisor自启动
supervisor自启动 其实自启动,也就是在主机开启的时候,执行了sudo supervisord -c /etc/supervisord.conf: 创建/usr/lib/systemd/sys ...
- new FileInputStream(file)中file不能为文件夹
今天闲来无事,写了如下程序: package com.xhj.test; import java.io.File; import java.io.FileFilter; import java.io. ...
- 我购买byd的几点逻辑
1.伯克希尔哈撒韦长期看好byd不是无道理的,每次转型都是那么的成功,说明管理层很好. 2.2015年6月员工持股计划均价55元,目前48元. 3.新能源汽车龙头. 4.云轨解决了小城市建设地铁过于浪 ...
- div内容超出后自动显示滚动条
一. <div style=" overflow:scroll; width:400px; height:400px;”></div> 记住宽和高一定要设置噢,否则不 ...
- 多角度看.NET面试题
1.ASP.NET中的身份验证有那些?你当前项目采用什么方式验证请解释 身份验证是从用户获取名称和密码等标识凭证并根据某些机构验证这些凭据的过程.如果凭据有效,则提交该凭据的实体被视为通 ...
- golang sql.DB
数据库 sql.DB连接池需知: sql.DB内置连接池,连接不足时会自动创建新连接,新创建的连接使用sql.Open()时传入的dsn来构造. sql.DBClose时只会关闭连接池中的连接,未归还 ...