Codeforces 682C Alyona and the Tree
题目链接: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的更多相关文章
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- 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 ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
- codeforces 682C Alyona and the Tree DFS
这个题就是在dfs的过程中记录到根的前缀和,以及前缀和的最小值 #include <cstdio> #include <iostream> #include <ctime ...
- CodeForces 682C Alyona and the Tree(广搜 + 技巧)
方法:从根节点开始广搜,如果遇到了应该删除的点,就再广搜删掉它的子树并标记,然后统计一下被标记的个数就是答案,所谓技巧就是从根节点开始搜索的时候,如果遇到了某个节点的距离<0,就让它是0,0可以 ...
- CodeForces 682C Alyona and the Tree (树上DFS)
题意:给定一棵树,每个叶子有一个权值,每条边也有一个权值,现在让你删最少的结点,使得从任何结点出发到另一个结点的边上权值和都小于两个结点的权值. 析:很明显是DFS,不过要想找出最少的结点可能不太容易 ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- Berry Jam (前缀和)cf教育场
距离上一篇博客又2个月了 寻思着该除除草了 教育场是真的好难 可能是因为我还是只菜鸡 哭唧唧 先说下题意:有2*n罐果酱,草莓和蓝莓,梯子在中间从梯子那开始往两边吃(可以一会左一会右),问最少吃多少果 ...
- poj 3436 ACM Computer Factory 最大流+记录路径
题目 题意: 每一个机器有一个物品最大工作数量,还有一个对什么物品进行加工,加工后的物品是什么样.给你无限多个初始都是000....的机器,你需要找出来经过这些机器操作后最多有多少成功的机器(111. ...
- Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math (数学)
题意:给你一个正整数\(n\),求两个正整数\(a\)和\(b\),使得\(a+b=n\),并且\(LCM(a,b)\)要尽可能的小. 题解:首先对于偶数,构造\(\frac{n}{2}\)和\(\f ...
- 缓冲区溢出实验 1 strcpy
实验代码 https://github.com/TouwaErioH/security/tree/master/stack%20overflow 实验目的 Buffer over flow 漏洞利用实 ...
- oslab oranges 一个操作系统的实现 实验五 让操作系统走进保护模式
实验目的: • 如何从软盘读取并加载一个Loader程序到操作 系统,然后转交系统控制权 • 对应章节:第四章 实验内容: 1. 向软盘镜像文件写入一个你指定的文件,手 工读取在磁盘中的信息 2. 在 ...
- cobaltstrike的使用
0x01 介绍 Cobalt Strike是一款渗透测试神器,常被业界人称为CS神器.Cobalt Strike已经不再使用MSF而是作为单独的平台使用,它分为客户端与服务端,服务端是一个,客户端可以 ...
- μC/OS-III---I笔记6---互斥信号量
互斥信号量 操作系统中利用信号量解决进程间的同步和互斥(互斥信号量)的问题,在多道程序环境下,操作系统就是遮掩实现进程之间的同步和互斥.但是在使用的过程中厉害的前辈还是发现了这一优秀机制的缺陷,它会导 ...
- Ubuntu pppoeconf失败
之前是通过sudo pppoeconf一路yes就可以连通有线网络(dsl和ethernet)的, 系统再次瘫痪后终于进入图形界面, 有线网络丢失, sudo pppoeconf也fail了, 其实加 ...
- TypeScript TSConfig All In One
TypeScript TSConfig All In One tsconfig.json https://www.typescriptlang.org/tsconfig https://www.typ ...
- tree ignore & bash & cmd
tree ignore & bash & cmd tree ignore https://unix.stackexchange.com/a/47806 https://zaiste.n ...