CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path
- 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值.
- 任意时刻权值非负的限制可以不用管,因为若走路径 \(u\to v\) ,走到 \(w\) 权值为负数了,那么直接从 \(w\) 下一个点开始走显然更优.这个限制是多余的.
- 那么问题就很简单了,经典 \(dp\) 做法,记 \(f(i)\) 为 \(i\) 子树内一点到 \(i\) 所有路径中的最大权值, \(O(n)\) 即可解决问题.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline int read()
{
int x=0;
bool pos=1;
char ch=getchar();
for(;!isdigit(ch);ch=getchar())
if(ch=='-')
pos=0;
for(;isdigit(ch);ch=getchar())
x=x*10+ch-'0';
return pos?x:-x;
}
const int MAXN=6e5+10;
int cnt,head[MAXN],to[MAXN<<1],nx[MAXN<<1];
int w[MAXN];
inline void addedge(int u,int v)
{
++cnt;
to[cnt]=v;
nx[cnt]=head[u];
head[u]=cnt;
swap(u,v);
++cnt;
to[cnt]=v;
nx[cnt]=head[u];
head[u]=cnt;
}
int n;
ll f[MAXN];
ll ans=0;
void upd(ll x,ll &mx,ll &sc)
{
if(x>=mx)
sc=mx,mx=x;
else if(x>sc)
sc=x;
}
void dfs(int u,int fa)
{
ll mx=0,sc=0;
for(int i=head[u];i;i=nx[i])
{
int v=to[i];
if(v==fa)
continue;
dfs(v,u);
upd(f[v],mx,sc);
}
f[u]=mx+w[u];
ans=max(mx+sc+w[u],ans);
}
int main()
{
n=read();
for(int i=1;i<=n;++i)
w[i]=read();
for(int i=1;i<n;++i)
{
int u=read(),v=read();
addedge(u,i+n);
addedge(v,i+n);
w[i+n]=-1*read();
}
dfs(1,0);
cout<<ans<<endl;
return 0;
}
CF1083A The Fair Nut and the Best Path的更多相关文章
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path
D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- CodeForces 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- D. The Fair Nut and the Best Path 树形dp (终于会了)
#include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int dp[maxn] ...
- 【Codeforces 1083A】The Fair Nut and the Best Path
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们最后要的是一条最长的路径. 这条路径的权值和是所有点的权值和-所有边的权值和且这个值最大. 显然如果我们在某一条边上的累计的权值和< ...
- Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费
题目大意: 给定一棵树 树上每个点有对应的点权 树上每条边有对应的边权 经过一个点可得到点权 经过一条边必须花费边权 即从u到v 最终得分=u的点权-u到v的边权+v的点权 求树上一条路径使得得分最大 ...
- CF1083E The Fair Nut and Rectangles
CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
随机推荐
- linux怎么上真正的国际互联网
1.安装git yum install -y git 2.执行git clone git@github.com:XX-net/XX-Net.git 3.升级python到python 2.7(妈妈说p ...
- grafana二次开发
grafana官方地址: https://github.com/grafana/grafana 开发文档:http://docs.grafana.org/project/building_from_s ...
- Vuex访问状态对象的方法
除了<Vuex最基本样例>中的方法外,还有两种方法访问状态对象state: 只需要改app.vue文件 方法一:引入computed <template> <div id ...
- java 位数补0处理(转)
数据库中表的ID为4位数字,如:0001,0012,0123,1234 如果插入的值不满足4位,需要进行转换,有两种方法 第一种方法以: int n = 1; NumberFormat ...
- 如何优雅地使用 rm 防止误删除?
IT 界的有一个老梗,一次某论坛的数据库管理员抱怨自己老板一直虐待他,结果他一气之下就删库跑路了...... 于是... 据新华社北京 8 月 20 日电 ,北京一软件工程师徐某离职后因公司未能如期结 ...
- ctci1.4
; ; ; i < len ; i++) ; +]; ; i < len; i++){ '; } ...
- Python list降序排序
test = [6,1,2,3,4,5] a = sorted(test,reverse=True) print a 结果如下: [6, 5, 4, 3, 2, 1] 你可以参考下sorted,里面是 ...
- Spring Boot Logback几种日志详解
日志对于应用程序来说是非常重要的,Spring框架本身集成了不少其他工具,我们自身的应用也会使用到第三方库,所以我们推荐在Spring应用中使用SLF4J/Logback来记录日志. SLF4J与Lo ...
- Django 前后台的数据传递示列
Django 前后台的数据传递的方法 Django 从后台往前台传递数据时有多种方法可以实现. 最简单的后台是这样的: ? 1 2 3 4 from django.shortcuts import r ...
- Highcharts 时间序列,可缩放的图表;Highcharts X 轴翻转曲线图;Highcharts 带标记曲线图
Highcharts 时间序列,可缩放的图表 配置 图表 配置可缩放图表. chart.zoomType 指定了用户可以拖放的尺寸,用户可以通过拖动鼠标来放大,可能值是x,y或xy: var char ...