poj 3728 The merchant(LCA)
Description
There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path. When he move along a path, he can choose one city to buy some goods and sell them in a city after it. The goods in all cities are the same but the prices are different. Now your task is to calculate the maximum possible profit on each path.
Input
The first line contains N, the number of cities.
Each of the next N lines contains wi the goods' price in each city.
Each of the next N- lines contains labels of two cities, describing a road between the two cities.
The next line contains Q, the number of paths.
Each of the next Q lines contains labels of two cities, describing a path. The cities are numbered from to N. ≤ N, wi, Q ≤
Output
The output contains Q lines, each contains the maximum profit of the corresponding path. If no positive profit can be earned, output instead.
Sample Input
Sample Output
Source
题意很简单
给一个树(n < 5w) 每个点有个权值,代表商品价格
若干个询问(5w)
对每个询问,问的是从u点走到v点(简单路径),商人在这个路径中的某点买入商品,然后在某点再卖出商品, 最大可能是多少
注意一条路径上只能买卖一次,先买才能卖
用的方法是离线LCA,在上面加了一些东西
对于一个询问, 假设u,v的LCA是f
那么有三种可能, 一个是从u到f 买卖了。 一个是从f到v买卖了, 一个是从u到f之间买了,从v到f卖了
从u到f 我们称为up, 从f到v我们称为down,而从u到f买然后在f到v卖就是求u到f的最小值和f到v的最大值。
我们要分别求这三个值
实际上就是在离线tarjan求LCA的过程中求出的
对于每个点, 我们进行dfs的时候,查看于其相关的询问,
假设当前点是u, 询问的点是v, u和v的LCA是f,如果v已经dfs过了,说明v在并查集中的祖先就是u,v的LCA f点,
将该询问加入到f的相关集合中,等f所有的子节点都处理过后再去处理f, 就可以发现,一切都是顺其自然了
在这些处理过程中,up和down以及u,v到f的最大值最小值 都可以在并查集求压缩路径的过程中更新。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <map>
#include <ctime>
#define MAXN 52222
#define MAXM 222222
#define INF 1000000001
using namespace std;
vector<int>g[MAXN], st[MAXN], ed[MAXN], id[MAXN], ask[MAXN], pos[MAXN];
int mx[MAXN], mi[MAXN], up[MAXN], down[MAXN], vis[MAXN], fa[MAXN], ans[MAXN];
int n, Q;
int find(int x) {
if(x == fa[x]) return x;
int y = fa[x];
fa[x] = find(y);
up[x] = max(up[x], max(mx[y] - mi[x], up[y]));
down[x] = max(down[x], max(mx[x] - mi[y], down[y]));
mx[x] = max(mx[x], mx[y]);
mi[x] = min(mi[x], mi[y]);
return fa[x];
}
void tarjan(int u) {
vis[u] = ;
for(int i = ; i < ask[u].size(); i++) {
int v = ask[u][i];
if(vis[v]) {
int t = find(v);
int z = pos[u][i];
if(z > ) {
st[t].push_back(u);
ed[t].push_back(v);
id[t].push_back(z);
} else {
st[t].push_back(v);
ed[t].push_back(u);
id[t].push_back(-z);
}
}
}
for(int i = ; i < g[u].size(); i++) {
int v = g[u][i];
if(!vis[v]) {
tarjan(v);
fa[v] = u;
}
}
for(int i = ; i < st[u].size(); i++) {
int a = st[u][i];
int b = ed[u][i];
int t = id[u][i];
find(a);
find(b);
ans[t] = max(up[a], max(down[b], mx[b] - mi[a]));
}
} int main() {
scanf("%d", &n);
int u, v, w; for(int i = ; i <= n; i++) {
scanf("%d", &w);
mx[i] = mi[i] = w; fa[i] = i;
}
for(int i = ; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u); }
scanf("%d", &Q);
for(int i = ; i <= Q; i++) {
scanf("%d%d", &u, &v);
ask[u].push_back(v);
pos[u].push_back(i);
ask[v].push_back(u);
pos[v].push_back(-i);
}
tarjan();
for(int i = ; i <= Q; i++) printf("%d\n", ans[i]); return ;
}
poj 3728 The merchant(LCA)的更多相关文章
- POJ 3728 The merchant(LCA+DP)
The merchant Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
- POJ 3728 The merchant (树形DP+LCA)
题目:https://vjudge.net/contest/323605#problem/E 题意:一棵n个点的树,然后有m个查询,每次查询找(u->v)路径上的两个数,a[i],a[j],(i ...
- POJ 3728 The merchant(并查集+DFS)
[题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...
- poj 3728 The merchant 倍增lca求dp
题目: zdf给出的题目翻译: 从前有一个富饶的国度,在这里人们可以进行自由的交易.这个国度形成一个n个点的无向图,每个点表示一个城市,并且有一个权值w[i],表示这个城市出售或收购这个权值的物品.又 ...
- poj 1986 Distance Queries(LCA)
Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...
- 洛谷P3379 【模板】最近公共祖先(LCA)
P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交 讨论 题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...
- 图论--最近公共祖先问题(LCA)模板
最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...
- POJ 3669 Meteor Shower(流星雨)
POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS Memory Limit: 65536K Description 题目描述 Bessie hears ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
随机推荐
- Oracle判断语句集合(转载)
SELECT decode(sign(to_date('2008-05-01', 'yyyy-MM-dd') - to_date('2008-03-01', 'yy ...
- SqlServer经典函数之数字去零
需求: 针对带有小数点的数字信息,去除小数点后多余的零 可能存在的情况: 1.精度范围内,出现多余的零 eg:1234.3400 想要的结果为1234.34 2.精度变大出现的多余的零, ...
- ASP.NET内核几大对象、ASP.NET核心知识(7)--转载
本文的学习流程是这样安排的. 一个简单的GDI小案例 1.说明 如果你想思考如何生成验证码,那么您第一个要解决的问题,一定是.NET动态生成图片问题. //GDI:.Net程序中进行绘图的一些类. 2 ...
- EffectiveC#3--选择is或者as操作符而不是做强制类型转换
1.用as运算符进行类型转换.因为比起盲目的强制转换它更安全,而且在运行时效率更高. 安全体现在:as操作符就算是转化一个null的引用时,也会安全的返回一个null而不会像强制转换抛出异常. 2.a ...
- 面试前的准备---C#知识点回顾----05
技术博客还得继续写,工作还在筛选,学习还得继续 1.Session和Cookie的使用区别 很容易回答的就是Session在服务器端,存储的数据可以较大容量,比如我们存一个Table,上千条数据. C ...
- JS操作URL
function getQueStr(url, ref) //取获参数值 { ); ) { var arr = str.split('&'); for (i in arr) { ] == re ...
- FULL JOIN 与 CROSS JOIN
FULL JOIN 只要其中某个表存在匹配,FULL JOIN 关键字就会返回行.(返回JOIN 两端表的所有数据,无论其与另一张表有没有匹配.显示左连接.右连接和内连接的并集) FULL JOIN ...
- 使用oracle来计算方差及标准差
/* Formatted on 5/24/2012 4:15:58 PM (QP5 v5.149.1003.31008) */ SELECT deptno, ename, ...
- Visual Studio .NET、.NET Framework和C#之间的联系
Visual Studio .NET是一种集成开发环境(IDE),它包含3种高级程序设计语言,C#就是其中的一种:Visual Studio .NET之所以能把这三种语言有机结合起来并具有与平台无关的 ...
- poj1458 求最长公共子序列 经典DP
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45763 Accepted: 18 ...