链接:

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,树,思维)的更多相关文章

  1. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  2. codeforces 1076E Vasya and a Tree 【dfs+树状数组】

    题目:戳这里 题意:给定有n个点的一棵树,顶点1为根.m次操作,每次都把以v为根,深度dep以内的子树中所有的顶点(包括v本身)加x.求出最后每个点的值为多少. 解题思路:考虑到每次都只对点及其子树操 ...

  3. DFS/BFS+思维 HDOJ 5325 Crazy Bobo

    题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...

  4. Codeforces962F Simple Cycles Edges 【双连通分量】【dfs树】

    题目大意: 给出一个无向图,问有哪些边只属于一个简单环. 题目分析: 如果这道题我们掌握了点双连通分量,那么结论会很显然,找到每个点双,如果一个n个点的点双正好由n条边构成,那么这些边都是可以的. 这 ...

  5. zstu.4191: 无向图找环(dfs树 + 邻接表)

    4191: 无向图找环 Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 117  Solved: 34 Description 给你一副无向图,每条边有 ...

  6. BZOJ_4238_电压_树上差分+dfs树

    BZOJ_4238_电压_树上差分+dfs树 Description 你知道Just Odd Inventions社吗?这个公司的业务是“只不过是奇妙的发明(Just Odd Inventions)” ...

  7. LOJ2250 [ZJOI2017] 仙人掌【树形DP】【DFS树】

    题目分析: 不难注意到仙人掌边可以删掉.在森林中考虑树形DP. 题目中说边不能重复,但我们可以在结束后没覆盖的边覆盖一个重复边,不改变方案数. 接着将所有的边接到当前点,然后每两个方案可以任意拼接.然 ...

  8. BZOJ5203 [NEERC2017 Northern] Grand Test 【dfs树】【构造】

    题目分析: 首先观察可知这是一个无向图,那么我们构建出它的dfs树.由于无向图的性质我们可以知道它的dfs树只有返祖边.考虑下面这样一个结论. 结论:若一个点的子树中(包含自己)有两个点有到它祖先的返 ...

  9. Leha and another game about graph CodeForces - 840B (dfs)

    链接 大意: 给定无向连通图, 每个点有权值$d_i$($-1\leq d_i \leq 1$), 求选择一个边的集合, 使得删除边集外的所有边后, $d_i$不为-1的点的度数模2等于权值 首先要注 ...

随机推荐

  1. linux 系统下删除tar.gz的tar

    在终端输入: 1.删除环境配置 vi /etc/profile 删除对应的jdk配置. 2.删除jvm rm /usr/lib/jvm 3.删除对应的tar解压的java目录. 4.输入 java 或 ...

  2. zabbix报警后不会自动消除解决

    http://www.cnblogs.com/zhongkai-27/p/9984597.html

  3. Activity启动场景Task分析(二)

    场景分析 下面通过启动Activity的代码来分析一下: 1.桌面 首先,我们看下处于桌面时的状态,运行命令: adb shell dumpsys activity 结果如下 ACTIVITY MAN ...

  4. 关机报 at-spi-bus-launcher

    查看是否有autostart文件夹,有则不建立 mkdir -v ~/.config/autostart 可直接修改/etc/xdg/autostart/at-spi-dbus-bus.desktop ...

  5. FileSystemWatcher监听文件事件

    现有一个需求如下:监控某个目录中的文件修改,创建,删除等信息,并记录下来. 这里用到FileSystemWatcher类.由于考虑到文件的写入量会很频率,所以考虑先将监听到的消息记录到内存中. 监听部 ...

  6. es5实现数组去重

    var a = [1, 2, 4, 4, 3, 3, 1, 5, 3] console.log(a.filter((item, index, arr) => { return arr.index ...

  7. 模板变量设置 set 和 with

    from flask import Flask,render_template app = Flask(__name__) @app.route('/') def hello_world(): ret ...

  8. Java实验报告四

    一.实验目的 (1)掌握类的继承方法: (2)变量的继承和覆盖,方法的继承.重载和覆盖实现: 二.实验内容 1)实验代码 import java.util.Scanner; public class ...

  9. Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 的解决办法

    eclipse导入mavn工程报Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 的解决办法: 错 ...

  10. C语言--- 高级指针2(结构体指针,数组作为函数参数)

    一.结构体指针 1. 什么是结构体指针?指向结构体变量的指针     结构体:     typedef  struct stu{                          char name[ ...