【Codeforces 1083A】The Fair Nut and the Best Path
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
我们最后要的是一条最长的路径。
这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大。
显然如果我们在某一条边上的累计的权值和<0了,那么我们会发现,我们完全没有必要一直累加到这条边,直接从边的另外一个端点开始重新累加更好(这时累加和>=0)
所以如果我们求的是最大的权值和-边权和的话,那么求出来的路径一定不会有中间某个地方走着走着没油的情况
因此我们只要按照树上最长链的类似方法。
求出来,从i的不同子树下的节点到达i节点的点权和减去边权和的最大值和次小值。
对于所有的点,用这两个值的和尝试更新答案即可。
【代码】
#include <bits/stdc++.h>
#define ll long long
using namespace std;const int N = 3e5;int n;
int a[N+10];
ll f[N+10][2],ans=0;vector<pair<int,int> > g[N+10];void dfs(int x,int pre){int len = g[x].size();f[x][0] = a[x];for (int i = 0;i < len;i++){int y = g[x][i].first,cost = g[x][i].second;if (y==pre) continue;dfs(y,x);if (f[x][0]<f[y][0]-cost+a[x]){f[x][1] = f[x][0];f[x][0] = f[y][0]-cost+a[x];}else{if (f[x][1]<f[y][0]-cost+a[x]){f[x][1] = f[y][0]-cost+a[x];}}}if (f[x][1]>0){ans = max(ans,f[x][0]+f[x][1]-a[x]);}else ans = max(ans,f[x][0]);}int main(){ios::sync_with_stdio(0),cin.tie(0);cin >> n;for (int i = 1;i <= n;i++) cin >>a[i];for (int i = 1;i <= n-1;i++){int x,y,z;cin >> x >> y >> z;g[x].push_back({y,z});g[y].push_back({x,z});}dfs(1,-1);cout<<ans<<endl;return 0;
}
【Codeforces 1083A】The Fair Nut and the Best Path相关推荐
- 【CodeForces - 1084D】The Fair Nut and the Best Path (树形dp)
题干: The Fair Nut is going to travel to the Tree Country, in which there are nn cities. Most of the l ...
- 【CodeForces - 1084C】The Fair Nut and String(思维,组合数学)
题干: The Fair Nut found a string ss. The string consists of lowercase Latin letters. The Nut is a cur ...
- codeforces 1083 A. The Fair Nut and the Best Path(树形dp)
题目大意: 每个节点都给定一个值a[i],从一个节点走到另一个节点会消耗固定值w,但也会得到这个节点的价值,问怎样走才能得到最大的价值. 解题思路: 这个题和树形dp求树的直径差不多(树形DP基本都是 ...
- 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)
题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...
- 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)
题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...
- 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)
题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...
- 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)
题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...
- 【CodeForces 997C】Sky Full of Stars(组合计数)
题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...
- CF 1083 A. The Fair Nut and the Best Path
A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...
最新文章
- 【JS基础】类型转换——不同数据类型比较
- hdu2159 FATE
- 波特率与比特率的关系
- WebService大讲堂之Axis2(2):复合类型数据的传递
- 使用nomad部署mysql
- 1130-host ... is not allowed to connect to this MySql server登录失败
- 有了这三个神器工具集,应用开发想怎么玩就怎么玩
- 中国晶体谐振器行业市场供需与战略研究报告
- OpenStack 已死?
- 【转】女人最想要的是什么
- CentOS 7 samba 配置
- Spring已集成jsp的环境下同时集成Velocity
- layer时间插件laydate
- 初探信息科学中“三个世界”模型
- 普元mobile_普元Primeton Mobile 7.1 正式发布 互联网集成能力加速企业数字化转型
- VSCode常用插件和快捷键总结
- 正则表达式元字符查询
- 刘鹏教授接受新华日报财经客户端采访:智能制造应用落地生根,“江苏智造”进入快速增长期...
- 解读|TARS开源项目发布Go语言版本 1
- C++调用opencv完成运动目标捕捉
热门文章
- 【03yy and one】
- html电脑自动输出什么意思code,VScode自动生成HTML的含义
- 我的世界HMCL启动器以及加入服务器步骤
- 每天一个PS技巧(原理+实践)——制作熊猫人表情包
- 拉取项目pom文件报错,jai_core-1.1.3.jar,解决jar引入问题
- Apache2.2+MySql5.5+PHP5.4的安装和配置(windows)
- css圆环进度条的几种方法
- RabbitMQ图文详解 | MQ_SpringAMQP | 系统性学习 | 无知的我费曼笔记
- 怎么避免域名被微信封杀,微信域名防封需要注意哪些问题。
- KMP算法之next数组详解