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. Java事件处理机制2

    实现一个小程序,怎样让小球受到键盘的控制,上下左右移动,如图: public class Demo3 extends JFrame{ MyPanel mp=null; public static vo ...

  2. 解析浏览器和nodejs环境下console.log()的区别

    写在前面的 在开发调试过程中,我们经常需要调用console.log 方法来打印出当前变量的值,然而,console.log在浏览器环境下 有时会出现一些异常的现象 开撸代码 在浏览器和nodejs环 ...

  3. pagination使用说明

    参考自张鑫旭 准备工作 下载jquery.min.js 下载jquery.pagination.js 下载pagination.css 开始敲代码 第一步,引入刚刚下载的三个文件 <link r ...

  4. 复制excel表,往excel表中写入数据

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import jav ...

  5. 贰、js的基础(一)

    1.js的语法 a.区分大小写 b.弱类型变量:变量无特定类型 c.每行结尾的分号可有可无 d.括号用于代码块 e.注释的方法与c语言和java相同 2.变量 注意事项: a.通过关键字var来声明. ...

  6. git远程仓库变更

    查看自己的远程仓库 git remote -v 远程仓库变更 git remote remove origin //移出现有的远程仓库的地址 git remote add origin http:// ...

  7. Node_进阶_4

    Node进阶第四天 一.传统数据库技术回顾 数据库就是存储数据的,那么存储数据用txt就行了啊,为什么要有数据库? 理由之一:数据库有行.列的概念,数据有关系,数据不是散的. 老牌数据库,比如Mysq ...

  8. Linux 操作基础(一) -- Shell 命令格式和元字符

    1 命令格式 cmd [-选项] [参数] 说明: • 最简单的Shell命令只有命令名,复杂的Shell命令可以有多个选项和参数 • 参数是文件也可以是目录,有些命令必须使用多个操作对象 • 并非所 ...

  9. 每个人都能实现的vue自定义指令

    前文 先来bb一堆废话哈哈.. 用vue做项目也有一年多了.除了用别人的插件之外.自己也没尝试去封装指令插件之类的东西来用. 刚好最近在项目中遇到一个问题.(快速点击按钮多次触发多次绑定的方法),于是 ...

  10. svn文件管理器的使用

    服务器端: 客户端 使用SVN的注意事项 做任何操作之前,先update一下 不要修改其他人的文件 不要在SVN里直接打开.编辑文件 不要在打开.编辑文件的时候,进行操作 SVN客户端的安装,非常简单 ...