题目链接:http://codeforces.com/problemset/problem/682/C

分析:存图,用dfs跑一遍,详细见注释

 1 #include<iostream>
2 #include<sstream>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<string>
6 #include<cstring>
7 #include<algorithm>
8 #include<functional>
9 #include<iomanip>
10 #include<numeric>
11 #include<cmath>
12 #include<queue>
13 #include<vector>
14 #include<set>
15 #include<cctype>
16 #define PI acos(-1.0)
17 const int INF = 0x3f3f3f3f;
18 const int NINF = -INF - 1;
19 const int maxn = 1e5 + 5;
20 typedef long long ll;
21 using namespace std;
22 int n;
23 int arr[maxn];
24 struct edge
25 {
26 int to, cost;
27 };
28 vector<edge> G[maxn];
29 int dfs(int x, int y, ll dis)//x为dfs当前进行到的节点,y为x节点的父亲节点,dis记录从根结点到当前x点的距离
30 {
31 if (dis > arr[x]) return 0;//如果触发到距离大于节点权值就返回0
32 int ans = 1;//ans记录有几个剩余节点,每一个节点的ans只有0或1,0即舍去
33 for (int i = 0; i < G[x].size(); ++i)
34 {
35 if (G[x][i].to == y) continue;//防止dfs返回到x节点的父亲节点
36 ans += dfs(G[x][i].to, x, max(G[x][i].cost + dis, 0ll));//因为题目中存在负权边,如果累加负数,显然会抵消正数造成答案出错
37 }//反证:例如,1-3的dis为-10,如果累加了这个-10,3-4的为5,而4权值为3,那么dis1-4为-5小于3,节点4不需要删除,但这显然是错的
38 return ans;
39 }
40 int main()
41 {
42 cin >> n;
43 for (int i = 1; i <= n; ++i)
44 cin >> arr[i];
45 for (int i = 2; i <= n; ++i)
46 {
47 int to, cost;
48 cin >> to >> cost;
49 G[i].push_back(edge{to, cost});//存图
50 G[to].push_back(edge{i, cost});
51 }
52 int ans = dfs(1, -1, 0);//从根节点1开始进行dfs
53 cout << n - ans;
54 return 0;
55 }

Codeforces 682C Alyona and the Tree的更多相关文章

  1. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  2. 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 ...

  3. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  4. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  5. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

  6. codeforces 682C Alyona and the Tree DFS

    这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...

  7. CodeForces 682C Alyona and the Tree(广搜 + 技巧)

    方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...

  8. CodeForces 682C Alyona and the Tree (树上DFS)

    题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...

  9. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. Codeforces Round #570 (Div. 3) E. Subsequences (easy version) (搜索,STL)

    题意:有一长度为\(n\)的字符串,要求得到\(k\)不同的它的子序列(可以是空串),每个子序列有\(|n|-|t|\)的贡献,求合法情况下的最小贡献. 题解:直接撸个爆搜找出所有子序列然后放到set ...

  2. Chip Factory HDU - 5536 字典树(删除节点|增加节点)

    题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...

  3. python常用连接字符串

    1.使用占位符% print(('%s%s%s' % ('one','two', 'three'))) 2.'+'号连接 字符串是不可变对象,每次改变会申请一块新的内存,操作符+连接字符串的时候会涉及 ...

  4. Shell 编程练习

    将后缀名为 .txt 的文件改成 .log [root@k8s-master test]# touch localhost_2020-01-{02..26}.txt [root@k8s-master ...

  5. C# 数据类型(2)

    String char的集合 string name = "John Doe"; 双引号,char是单引号string是不可变的,一旦初始化后就不能变了,每次对已存在的string ...

  6. Asp.Net Core Grpc 入门实践

    Grpc简介 gRPC 是一种与语言无关的高性能远程过程调用 (RPC) 框架. 在 gRPC 中,客户端应用程序可以直接调用不同计算机上的服务器应用程序上的方法,就像它是本地对象一样,从而更轻松地创 ...

  7. PHP 标记官方简明教程; <?php 和 ?> 与 <?php 的使用场景!

    PHP 标记官方简明教程:  <?php 和 ?> 与 <?php  的使用场景! 1 PHP 标记¶ 当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php 和  ...

  8. how to updating Node.js and npm

    how to updating  Node.js and npm 1 Installing Node.js and updating npm How do I update Node.js ? Not ...

  9. CSS3 Animation & Weather Icons

    CSS3 Animation & Weather Icons google fonts <link href='https://fonts.googleapis.com/css?fami ...

  10. js uppercase the first letter of string

    js uppercase the first letter of string js String.toUpperCase `-webkit-border-image`.split(`-`).filt ...