CodeForces-682C(DFS,树,思维)
链接:
https://vjudge.net/problem/CodeForces-682C
题意:
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?
思路:
给了一个有根树,那就建个有向图就行了,第一遍Dfs找到每个要删除的点,同时计算每个点的子节点有多少.
第二遍dfs把遇到的第一个要删的点,删除,同时他的子节点也要删除,因为可能出现根节点删除,子节点不删的情况.
所以直接统计根加子节点的数目一并删除.找满足删的点的时候要考虑是到他的任一祖先都满足,所以用一点dp的思想,
dis(u) 为到u点的最长值,dis(u) = max(dis(v)+w, w),不断更新即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;
struct Node
{
int to;
LL w;
};
LL Value[MAXN];
vector<Node> G[MAXN];
int Vis[MAXN], Sum[MAXN];
int n, cnt = 0;
int Dfs(int x, LL v)
{
int res = 1;
for (int i = 0;i < G[x].size();i++)
{
int to = G[x][i].to;
res += Dfs(to, max(v+G[x][i].w, G[x][i].w));
}
if (v > Value[x])
Vis[x] = 1;
Sum[x] = res;
return res;
}
void Dfs2(int x)
{
for (int i = 0;i < G[x].size();i++)
{
if (Vis[G[x][i].to])
cnt += Sum[G[x][i].to];
else
Dfs2(G[x][i].to);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int v, w;
cin >> n;
for (int i = 1;i <= n;i++)
cin >> Value[i];
for (int i = 1;i < n;i++)
{
cin >> v >> w;
G[v].push_back(Node{i+1, w});
}
Sum[1] = Dfs(1, 0);
Dfs2(1);
cout << cnt << endl;
return 0;
}
CodeForces-682C(DFS,树,思维)的更多相关文章
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- codeforces 1076E Vasya and a Tree 【dfs+树状数组】
题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】
题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...
- zstu.4191: 无向图找环(dfs树 + 邻接表)
4191: 无向图找环 Time Limit: 5 Sec Memory Limit: 128 MB Submit: 117 Solved: 34 Description 给你一副无向图,每条边有 ...
- BZOJ_4238_电压_树上差分+dfs树
BZOJ_4238_电压_树上差分+dfs树 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)” ...
- LOJ2250 [ZJOI2017] 仙人掌【树形DP】【DFS树】
题目分析: 不难注意到仙人掌边可以删掉.在森林中考虑树形DP. 题目中说边不能重复,但我们可以在结束后没覆盖的边覆盖一个重复边,不改变方案数. 接着将所有的边接到当前点,然后每两个方案可以任意拼接.然 ...
- BZOJ5203 [NEERC2017 Northern] Grand Test 【dfs树】【构造】
题目分析: 首先观察可知这是一个无向图,那么我们构建出它的dfs树.由于无向图的性质我们可以知道它的dfs树只有返祖边.考虑下面这样一个结论. 结论:若一个点的子树中(包含自己)有两个点有到它祖先的返 ...
- Leha and another game about graph CodeForces - 840B (dfs)
链接 大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值 首先要注 ...
随机推荐
- linux计划crontab
linux计划crontab 启动crontab服务 一般启动服务用 /sbin/service crond start 若是根用户的cron服务可以用 sudo service crond sta ...
- jmeter响应数据Unicode编码转换为汉字
2018-07-09 10:24:34 每次用jmeter做接口测试时,响应信息中文总是显示Unicode编码格式,每次都要在网上寻找这一段转换的代码,但是我发现在网上找这段代码有点麻烦,像我 ...
- leetcode之53.最大子序和
题目详情 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: ...
- CentOS7之root密码破解
1.重新启动Linux系统,在出现引导界面时上下移动光标选择第一引导项,按下键盘的“e”键进入内核编辑界面,如图所示: 2.找到linux16参数这一行,按住“Ctrl+e”组合键跳转到行尾,添加rd ...
- http请求跨域问题分析
http请求跨域问题分析 个人认为可能涉及很多http的知识,本人才疏学浅不敢妄自揣测,只是做个笔记为了以后对比理解,从原生fetch说起吧,前提假设有个后端服务,我用express来模拟,如下: v ...
- 文件的三种打开方式及with管理文件上下文
文件的三种打开方式及with管理文件上下文 一.文件的三种打开方式 1.1 只读 f = open(r'D:\pycharm\yjy\上海python学习\456.txt','r',encoding= ...
- python一行代码打印Love心形
用Python画一颗特别的爱心,送给那个特别的她,给她一份浪漫的惊喜吧 print('\n'.join([''.join([('Love'[(x-y) % len('Love')] if ((x*0. ...
- python格式化当前时间,暂停一秒输出
python格式化输出当前系统时间,可以实现暂停1秒输出时间 import timefor i in range(10): print(time.strftime("%Y-%m-%d %H: ...
- 反向传播算法-损失函数&激活函数
在监督学习中,传统的机器学习算法优化过程是采用一个合适的损失函数度量训练样本输出损失,对损失函数进行优化求最小化的极值,相应一系列线性系数矩阵W,偏置向量b即为我们的最终结果.在DNN中,损失函数优化 ...
- 手把手教你vue配置请求本地json数据
本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...