http://poj.org/problem?id=3728

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5068   Accepted: 1744

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-1 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 1 to N.

1 ≤ NwiQ ≤ 50000

Output

The output contains Q lines, each contains the maximum profit of the corresponding path. If no positive profit can be earned, output 0 instead.

Sample Input

4
1
5
3
2
1 3
3 2
3 4
9
1 2
1 3
1 4
2 3
2 1
2 4
3 1
3 2
3 4

Sample Output

4
2
2
0
0
0
0
2
0

Source

 
倍增维护4个值:到当前点的最大钱数,最小钱数,从深度小的点到深度大的点的最大利润,从深度大的点到深度小的点最大利润
统一答案时,分别从u到LCA(u,v),LCA(u,v)到v中找到最优解
(做过原题还WA好多次、、蒟蒻就是蒟蒻,)
 #include <cstdio>

 using namespace std;

 const int INF();
const int N(+);
int pri[N],sumedge,head[N];
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[N<<];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} #define swap(a,b) {int tmp=a;a=b;b=tmp;}
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
struct Type_Node
{
int up_down,down_up;
int maxx,minn,dad;
}shop[N][];
int deep[N];
void DFS(int x,int fa)
{
deep[x]=deep[fa]+;
shop[x][].maxx=max(pri[x],pri[fa]);
shop[x][].minn=min(pri[x],pri[fa]);
shop[x][].down_up=max(,pri[fa]-pri[x]);
shop[x][].up_down=max(,pri[x]-pri[fa]);
for(int i=;shop[x][i-].dad;i++)
{
shop[x][i].dad=shop[shop[x][i-].dad][i-].dad;
shop[x][i].maxx=max(shop[x][i-].maxx,shop[shop[x][i-].dad][i-].maxx);
shop[x][i].minn=min(shop[x][i-].minn,shop[shop[x][i-].dad][i-].minn);
shop[x][i].down_up=max(shop[x][i-].down_up,shop[shop[x][i-].dad][i-].down_up);
shop[x][i].down_up=max(shop[x][i].down_up,shop[shop[x][i-].dad][i-].maxx-shop[x][i-].minn);
shop[x][i].up_down=max(shop[x][i-].up_down,shop[shop[x][i-].dad][i-].up_down);
shop[x][i].up_down=max(shop[x][i].up_down,shop[x][i-].maxx-shop[shop[x][i-].dad][i-].minn);
}
for(int i=head[x];i;i=edge[i].next)
{
int v=edge[i].v;
if(shop[x][].dad!=v) shop[v][].dad=x,DFS(v,x);
}
}
int LCA(int x,int y)
{
if(deep[x]>deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[shop[y][i].dad]>=deep[x]) y=shop[y][i].dad;
if(x==y) return x;
for(int i=;i>=;i--)
if(shop[x][i].dad!=shop[y][i].dad) x=shop[x][i].dad,y=shop[y][i].dad;
return shop[x][].dad;
}
int Query(int x,int y)
{
int ans=,maxx=-INF,minn=INF,lca=LCA(x,y);
for(int i=;i>=;i--)
if(deep[shop[x][i].dad]>=deep[lca])
{
ans=max(ans,max(shop[x][i].down_up,shop[x][i].maxx-minn));
minn=min(minn,shop[x][i].minn);
x=shop[x][i].dad;
}
for(int i=;i>=;i--)
if(deep[shop[y][i].dad]>=deep[lca])
{
ans=max(ans,max(shop[y][i].up_down,maxx-shop[y][i].minn));
maxx=max(maxx,shop[y][i].maxx);
y=shop[y][i].dad;
}
return max(ans,maxx-minn);
} inline void read(int &x)
{
x=;register char ch=getchar();
for(;ch<''||ch>'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int main()
{
int n; read(n);
for(int i=;i<=n;i++) read(pri[i]);
for(int u,v,i=;i<n;i++)
{
read(u),read(v);
ins(u,v),ins(v,u);
}
DFS(,);
int q; read(q);
for(int u,v;q--;)
{
read(u),read(v);
if(u==v) puts("");
else printf("%d\n",Query(u,v));
}
return ;
}
 

POJ——T 3728 The merchant的更多相关文章

  1. [最近公共祖先] POJ 3728 The merchant

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 Desc ...

  2. poj 3728 The merchant(LCA)

    Description There are N cities in a country, and there is one and only one simple path between each ...

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

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

  4. poj——3728 The merchant

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5055   Accepted: 1740 Desc ...

  5. [POJ 3728]The merchant

    Description There are N cities in a country, and there is one and only one simple path between each ...

  6. POJ 3728 The merchant(LCA+DP)

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

  7. poj 3728 The merchant 倍增lca求dp

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

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

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

  9. POJ 3278:The merchant(LCA&DP)

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6864   Accepted: 2375 Desc ...

随机推荐

  1. hiho 171周 - 水题,并查集

    题目链接 题目描述: 输入4 alice 2 alice@hihocoder.com alice@gmail.com bob 1 bob@qq.com alicebest 2 alice@gmail. ...

  2. vue反向代理解决跨域

    问题描述 在项目开发的时候,接口联调的时候一般都是同域名下,且不存在跨域的情况下进行接口联调,但是当我们现在使用vue-cli进行项目打包的时候,我们在本地启动服务器后,比如本地开发服务下是 http ...

  3. [agc011e]increasing numbers

    题意: 如果一个十进制非负整数的所有数位从高位到低位是不减的,我们称它为“上升数”,例如1558,11,3,0都是上升数,而10,20170312则不是: 给定整数N,求最小的k使得N能被表示为k个上 ...

  4. du -sh*查看当前目录下的文件夹大小

    du -sh*查看当前目录下的文件夹大小   u 命令    用途    概述磁盘使用.    语法  du [ -a | -s ] [ -k ] [ -m ] [ -g ][ -l ] [ -r ] ...

  5. rem — 一个低调的css单位

    原文  http://www.zhaoan.org/1825.html rem这是个低调的 css 单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃 ...

  6. makefile--回顾基础篇

    前阵子让写makefile,纠结了下,基本忘记差不多了. 1.gcc的编译选项 -c 只是编译不链接,生成目标文件“.o” -S 只是编译不汇编,生成汇编代码 -E 只进行预编译,不做其他处理 -g ...

  7. hadoop-06-http服务

    hadoop-06-http服务 su root service httpd status service httpd stop vi /etc/httpd/conf/httpd.conf 修改:Do ...

  8. spring中操作mysql数据库

    就是在spring中,对mysql数据库进行增删改查的样例,很easy. 文件结构 maven的pom.xml文件,里面用到的几个很重要的jar包都有 <project xmlns=" ...

  9. SQLSever: 怎样在select中的每一行产生不同的随机数?

    select 的随机函数有点假, 或许是由于它是基于时间来的吧, 同一select中由于时间无法错开导致产生的随机数都是一样的. 怎样做到让不同的行拥有不同的随机数呢? 以下以产生某个月的随机日期来演 ...

  10. 实习第四天(bboss框架学习)

    现在好像比较使用的管理工具是gradle管理工具,学长说这个管理工具比maven管理工具要好用! 我今天主要就是想要安装好的gradle这个管理工具,但是可能是我的eclispe版本的问题,我没能安装 ...