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等于权值 首先要注 ...
随机推荐
- 分期花呗 账户交易通知:尾号6932客户,您的申请已通过,账户余额38139元,无手续费,点t.cn/Aijsx9vq取款,回T退订。
10692285499 分期花呗 账户变动通知:尾号6932客户,您的申请已通过,账户余额5000元,请及时点击t.cn/AiOMsNAm取款,回T退订. 106935276259002分期花呗 账户 ...
- python学习之数据类型(tuple)
3.6 元组 v = (11,22,33,'asd','汉字') 元组就是不可变的列表,又叫制度列表,属性特征与字符串相似,里边可以存放任何类型的元素. 1.元组的元素 这里元组的不可变的意思是⼦元素 ...
- centos7成功部署OpenLDAP
目录 一.部署OpenLDAP. 1 1.安装openLDAP. 1 2.设置openldap管理员密码... 1 3.更改openldap配置... 2 4.更改监控认证配置... 2 5.设置DB ...
- [Python3] 026 常用模块 calendar
目录 calendar 1. calendar.calendar(year, w, l, c, m) 2. calendar.prcal(year, w, l, c, m) 3. calendar.m ...
- 委托、泛型委托、多播委托、匿名函数、lamda表达式、事件
1.为什么要使用委托 将一个方法作为参数传递给另一个方法 2.委托概念 public delegate int 委托名(int a, int b); 声明一个委托类型,可以用访问修饰符修饰,deleg ...
- 如何用item pipeline(管道)清洗数据
版权声明:本文为博主原创文章,转载请注明出处:如果博客中有错误之处抑或有可以改进的地方,欢迎在评论区留言. https://blog.csdn.net/f156207495/article/detai ...
- [LeetCode] 226. 用队列实现栈
题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...
- UnknownPropertyException(Yii2)
在class里面的rule有属性,但是没声明
- springmvc中的数据传递
import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; impo ...
- javaScript运动框架之匀速运动
运动框架 1.在开始运动时,关闭已有定时器 2.把运动和停止隔开(if/else) 匀速运动的停止条件 运动终止条件:距离足够近 Demo代码 <!DOCTYPE html> <ht ...