题目链接: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. python爬取网易翻译 和MD5加密

    一.程序需要知识 1.python中随机数的生成 # 生成 0 ~ 9 之间的随机数 # 导入 random(随机数) 模块 import random print(random.randint(0, ...

  2. HihoCoder - 1055 树形dp

    vj链接:https://vjudge.net/contest/367007#problem/G 题意: 给你一棵树,树上有n个节点,每一个节点有一个权值,树根节点是1,你需要找到以1为起点连通的m个 ...

  3. XML、DTD约束

    XML的作用: xml现在主要用于配置文件 文档声明: 如果你使用记事本打开文档,此时如果记事本默认保存数据到硬盘根据的是"GB2312"编码,这个时候如果你在xml文档源码中en ...

  4. Distinct Substrings SPOJ - DISUBSTR 后缀数组

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  5. 002、Python中json字符串与字典转换

    1.测试用例文件TestCase.xlsx 2.编写Python文件进行读取 #!/usr/bin/env python # -*- coding:utf-8 -*- import time impo ...

  6. 001、Python数据结构

    #! usr/bin/env python # _*_ coding:utf-8 _*_ from random import randint ''' 一.list的增删改查 1.list.appen ...

  7. 缓冲区溢出实验 2 sizeof/strlen

    实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul2 下面只给出有漏洞部分的代码.Vul2问题为sizeof函数在计算时会考虑 ...

  8. HashMap三百问

    文章目录: 一.JDK1.7之HashMap 二.JDK1.8之HashMap 三.Hashtable JDK1.7之HashMap 1. 定义 HashMap实现了Map接口,继承AbstractM ...

  9. Git常用命令速查表 & Git Basics & github : release 发布!

    Git常用命令速查表 & Git Basics  & github : release  发布! Git常用命令速查表: 1 1 1 1 1 http://git-scm.com/bo ...

  10. ESLint & vue

    ESLint & vue { "name": "app", "version": "1.0.1", " ...