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

 
 
转自:http://m.blog.csdn.net/blog/sdj222555/43003179
 

题意很简单

给一个树(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)的更多相关文章

  1. POJ 3728 The merchant(LCA+DP)

    The merchant Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  2. POJ 3728 The merchant (树形DP+LCA)

    题目:https://vjudge.net/contest/323605#problem/E 题意:一棵n个点的树,然后有m个查询,每次查询找(u->v)路径上的两个数,a[i],a[j],(i ...

  3. POJ 3728 The merchant(并查集+DFS)

    [题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...

  4. poj 3728 The merchant 倍增lca求dp

    题目: zdf给出的题目翻译: 从前有一个富饶的国度,在这里人们可以进行自由的交易.这个国度形成一个n个点的无向图,每个点表示一个城市,并且有一个权值w[i],表示这个城市出售或收购这个权值的物品.又 ...

  5. 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 ...

  6. 洛谷P3379 【模板】最近公共祖先(LCA)

    P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交  讨论  题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...

  7. 图论--最近公共祖先问题(LCA)模板

    最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...

  8. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  9. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

随机推荐

  1. 【转】Android中自动连接到指定SSID的Wi-Fi

    最近在做一个项目,其中涉及到一块“自动连接已存在的wifi热点”的功能,在网上查阅了大量资料,五花八门,但其中一些说的很简单,即不能实现傻瓜式的拿来就用,有些说的很详细,但其中不乏些许错误造成功能无法 ...

  2. JSP通过SmartUpload上传文件实例

    httpRequest.setCharacterEncoding("gbk"); String preName = genName.doMake();//设置文件前缀名 Strin ...

  3. hdu 4400 Mines(离散化+bfs+枚举)

    Problem Description Terrorists put some mines in a crowded square recently. The police evacuate all ...

  4. 使用ssh远程执行命令批量导出数据库到本地

    前天正在跟前端的同事调试功能.服务器开好,模拟的玩家登录好,就在倒计时.这时突然运营的同事跑过来说要统计几个服务器玩家的一些情况,也就是需要从几个服的数据库导出部分玩家的数据.好吧,我看了一下时间,1 ...

  5. 面试时如何优雅的谈论OC

    在面试中,我们经常会遇到一些原理性的问题,很常识但很难用通俗的语言解释清楚,这也是大部分业务级程序员经常失误的地方.虽然写了多年代码,但是核心思想不清,导致自己的后续发展受限,这是一个优秀的程序员和普 ...

  6. Cocoa Pods的安装

    CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境.幸运的是OS X系统默认已经可以运行Ruby了,因此我们只需执行以下命令: sudo gem install -n /usr/ ...

  7. PC-HTML5-搜索框

    代码如下: <input type="text" placeholder="输入 回车搜索" autofocus x-webkit-speech>很 ...

  8. 关于java读取和写入properties配置文件的内容

    一般通过使用流的方式进行读取 代码示例如下: package com.zznode.transmit.util; import java.io.FileInputStream; import java ...

  9. CCA概述和安装

    什么是CCA? 客户关怀加速器(CCA)为微软动态®CRM通过集中的客户信息从不同的系统在一个集成代理桌面促进剂的效率和有效性. CCA是一个參考应用,利用用户界面集成(UII)为微软Dynamics ...

  10. jquery于form正在使用submit问题,未解决

    $("#login_btn").click(function(){type为submit的button下 if($("#id_password").val(). ...