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树,给定的两个字符串就 ...
随机推荐
- DataReader 连接数据库完整过程和代码(Sql Server)
数据库名叫:Bu 有个表:A 里面有一列:ID 需要引用 using System.Data.SqlClient; 代码部分如下: SqlConnection sqlCon=new SqlConnec ...
- enabled和priority属性
本篇来继续学习@Test下的注释,这篇学习两个属性的基本使用.第一个是设置该条用例不被执行,第二个的作用是设置用例执行的优先顺序. 1. 属性enabled 在Testng中,如果方法前面添加了@Te ...
- ElasticSearch + Canal 开发千万级的实时搜索系统【转】
公司是做社交相关产品的,社交类产品对搜索功能需求要求就比较高,需要根据用户城市.用户ID昵称等进行搜索. 项目原先的搜索接口采用SQL查询的方式实现,数据库表采用了按城市分表的方式.但随着业务的发展, ...
- Python 的selenium使用
一.Radio 判断radio是否已经被选中 driver.find_element_by_id().is_selected() 其返回值为bool类型,True or False 二.frame f ...
- ItemsControl的ItemContainerStyle属性
ItemsControl:ListBox,ComboBox,TreeView ItemContainerStyle是用来设置每一个集合控件的Item的样式的属性(即设置每一个项的样式). 使用It ...
- PWA web应用模型
2018年的第一篇博客,最近都去挤图书馆了,希望新年新气象... 简介 PWA 是一门Google推出的web前端新技术,全称是Progressive Web App,是Google在2015年提出, ...
- 25.大白话说java并发工具类-CountDownLatch,CyclicBarrier,Semaphore,Exchanger
1. 倒计时器CountDownLatch 在多线程协作完成业务功能时,有时候需要等待其他多个线程完成任务之后,主线程才能继续往下执行业务功能,在这种的业务场景下,通常可以使用Thread类的join ...
- 1-16-1 LVM管理和ssm存储管理器使用&磁盘配额
大纲: 1-1- LVM逻辑卷的管理 1-2- SSM管理工具的使用 1-3- 磁盘配额技巧 ====================================== 问题描述: 当我们需要在一个 ...
- laravel中composer镜像服务的方式
系统全局配置:即将配置信息添加到Composer的全局配置文件config.json中. 单个项目配置:将配置信息添加到某个项目的composer.json文件中. 例1:修改Composer的全局配 ...
- Oracle 使用小技巧
1.小数转换成字符往往会丢失前面的零. 解决方法: to_char(0.12345,'fm9999999990.00'); 2.除数为零的话oracle老是报错. 解决方法: decode(B,0,0 ...