CodeForces 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path
题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数。
题解:
dfs一遍, 求所有儿子走到这个点的最大值和次大值。
我们需要明白如果可以从u -> v 那么一定可以从 v -> u, 当然 指的是 u->v是路径上的最大和。
u->e1->v;
假如:val[u] = 100, val[e1] = 50, val[v] = 60, 那么我们发现可以从 u -> v 也可以从v -> u
val[u] = 100, val[e1] = 50, val[v] = 40, 虽然我们可以从u->v,但是 不能 v->u, 但是根据上面的定义,我们发现 从 u->v反而是亏本的,也就是说 u->u是最大的,我们不在考虑 u->v了。
val[u] = 40, val[e1] = 50, val[v] = 100, 和上面一样的道理。
所以,当一条路是最大的能赚的话, 那么一定可以走双向。
然后 现在还有一个疑问就是 如果从 u的父节点到u呢, 这个东西在 u往上传的时候就解决了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 3e5 + ;
const int M = *N;
int head[N], to[M], val[M], nt[M], tot;
void add(int u, int v, int w){
to[tot] = v;
val[tot] = w;
nt[tot] = head[u];
head[u] = tot++;
}
LL dp[N][];
int a[N];
LL ans = ;
LL dfs(int o, int u){
for(int i = head[u]; ~i; i = nt[i]){
int v = to[i];
if(v == o) continue;
LL tmp = dfs(u,v) - val[i];
if(tmp > dp[u][]) swap(tmp, dp[u][]);
if(tmp > dp[u][]) swap(tmp, dp[u][]);
}
ans = max(ans, dp[u][]+dp[u][]+a[u]);
return dp[u][] + a[u];
}
int main(){
int n, u, v, w;
scanf("%d", &n);
memset(head, -, sizeof(head));
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i < n; ++i){
scanf("%d%d%d", &u, &v, &w);
add(u,v,w);
add(v,u,w);
}
dfs(,);
cout << ans << endl;
return ;
}
CodeForces 1084D The Fair Nut and the Best Path的更多相关文章
- 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 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- Codeforces 1083E The Fair Nut and Rectangles
Description 有\(N\)个左下定点为原点的矩阵, 每个矩阵\((x_i,~y_i)\)都有一个数\(a_i\)表示其花费. 没有一个矩阵包含另一个矩阵. 现要你选出若干个矩阵, 使得矩阵组 ...
- Codeforces 1083B The Fair Nut and Strings
Description 给定两个由 \('a'\), \('b'\) 组成的字符串 \(a\), \(b\),以及两个整数 \(n\) 和 \(k\) \(n\) 表示字符串 \(a\),\(b\) ...
- 【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的点权 求树上一条路径使得得分最大 ...
- 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] ...
随机推荐
- 【MySQL】ON DUPLICATE KEY UPDATE
之前没用过这个操作,甚至没见过--最近接触到,而且还挺有用. 作用:若 KEY 不重复,则插入记录:否则更新记录. 单条操作: INSERT INTO table(a, b, c) VALUES (1 ...
- 【iOS】iOS CocoaPods 整理
github 上下载 Demo 时第一次遇到这个情况,当时有些不知所措,也没怎么在意,后来项目调整结构时正式见到了这个,并且自己去了解学习了. CocoaPods安装和使用教程 这篇文章写得很好!ma ...
- JDK的命令行工具系列 (二) javap、jinfo、jmap
javap: 反编译工具, 可用来查看java编译器生成的字节码 参数摘要: -help 帮助 -l 输出行和变量的表 -public 只输出public方法和域 -protected 只输出publ ...
- c#将字符串转化为合理的文件名
string name = System.Text.RegularExpressions.Regex.Replace(url, "[<>/\\|:\"?*]" ...
- Windows上的Linux容器
翻译自:https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/linux-contai ...
- 【POJ - 2236】Wireless Network (并查集)
Wireless Network 这接翻译了 Descriptions 地震发生在东南亚.ACM(亚洲合作医疗团队)已经与膝上电脑建立了无线网络,但是一次意外的余震袭击,网络中的所有计算机都被打破了. ...
- java虚拟机学习笔记(五)---运行时的数据区域
Java虚拟机所管理的内存包括以下几个运行时的数据区域:方法区,堆,虚拟机栈,本地方法栈,程序计数器.下面对其进行介绍: 程序计数器 它是一块较小的内存空间,可以看做当前线程做执行的字节码的信号指示器 ...
- axios配置请求头content-type
现在前端开发中需要通过Ajax发送请求获取后端数据是很普遍的一件事情了,鉴于我平时在撸码中用的是vue技术栈,今天这里来谈谈我们常用的发Ajax请求的一个插件—axios.> 现在网上可能发送A ...
- Java 复制PDF文档的2种方法
本文将介绍通过Java程序来复制PDF页面,包括: 跨文档复制,即从文档1复制到文档2 在同一文档内复制,即从页面A复制到页面B 使用工具:Free Spire.PDF for Java (免费版) ...
- ArrayList用法整理
System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度. 一.优点 1.支持自动改变大小的功能 2.可以灵活的插入元素 3.可以灵活的 ...