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等于权值 首先要注 ...
随机推荐
- nginx子配置文件实例
[root@bogon conf.d]# cat /etc/nginx/conf.d/test6.conf server { listen 8085; server_name 192.168.0.20 ...
- 在Windows服务器安装ss服务端用于逃脱公司行为管理
1.安装:python-2.7.14.amd64.msi 2.配置环境变量 3.Win64OpenSSL-1_0_2n.exe 4.安装ss服务端:pip install **adowsocks 5. ...
- Ubuntu虚拟机共享文件夹的1234
第一 在虚拟机内添加路径 进入虚拟机软件,点开工具栏上方虚拟机,点击设置,选择选项,查看共享文件夹,点击添加,下一步 第二: 第三 点击启用此共享 点击完成 第四 查看共享的文件 在mnt里可以看到S ...
- Tutorial : Implementing Django Formsets
A step-by-step tutorial for setting up and testing a standard Django formset. I’ve noticed on #djang ...
- ELK+Kafka日志收集环境搭建
1.搭建Elasticsearch环境并测试: (1)删除es的容器 (2)删除es的镜像 (3)宿主机调内存: 执行命令:sudo sysctl -w vm.max_map_count=655360 ...
- TensorFlow基础总结
1.基础概念 Tensor:类型化的多维数组,图的边:Tensor所引用的并不持有具体的值,而是保持一个计算过程,可以使用session.run()或者t.eval()对tensor的值进行计算. O ...
- CentOS7中SSH免密登陆设置
一.准备三台客户机 hadoop100 192.168.13.100(Master) hadoop101 192.168.13.101 hadoop102 192.168.13.102 二.在hado ...
- 【VS开发】【图像处理】Pleora推出iPORT CL-U3外置抓帧器
全球领先的高性能视频接口产品供应商Pleora科技公司近日宣布推出可将Camera Link®摄像头转化为USB3Vision™摄像头的首个产品iPORT CL-U3外置抓帧器,树立了另一个行业里程碑 ...
- selenium学习-模拟键盘按键操作
导入 from selenium.webdriver.common.keys import Keys 格式:Keys.XXX 一般这么用:send_keys(Keys.XXX) # coding= ...
- value(C# )
上下文关键字 value 用在普通属性声明的 set 访问器中. 此关键字类似于方法的输入参数. 关键字 value 引用客户端代码尝试分配给属性的值. 在以下示例中,MyDerivedClass 有 ...