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. swift语言点评十三-Lazy

    Lazy Stored Properties A lazy stored property is a property whose initial value is not calculated un ...

  2. SpringBoot学习笔记(7)-----CORS支持解决跨域问题

    在实际应用开发中,跨域是一个比较常见的问题,解决方法可以用jsonp,frame,cors等, 这里示例的是SpringBoot对CORS的支持的三种实现方式 第一种:配置一种全局的支持,这种方式需要 ...

  3. 深入了解JWT以及JWT的执行机制

    1.JWT以什么样的形式存在? eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9 ...

  4. 织梦dedecms支持flash的flv文件播放功能代码

    1.打开/include/FCKeditor/editor/dialog/dede_media.htm if(playtype=="rm"|| (playtype=="- ...

  5. vue.js的<slot>

    使用插槽分发内容在封装vue组件的时候,很多时候就不得不使用到vue的一个内置组件<slot>.slot是插槽的意思,顾名思义,这个<slot>组件的意义是预留一个区域,让其中 ...

  6. HTTP——学习笔记(7)

    HTTP中的认证机制 什么是认证机制?: 服务器需要知道客户端是谁. 怎样知道客户端身份?: 核对“登录者本人才知道的信息” 密码:只有本人才会知道的字符串信息 动态令牌:仅限本人持有的设备内显示的一 ...

  7. [转] 经典排序算法 - 基数排序Radix sort

    原理类似桶排序,这里总是需要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数 例如 待排序数组[62,14,59,88,16]简单点五个数字 分 ...

  8. ubuntu升级到14.04后终端显示重叠

    系统升级后,发现这个问题非常不爽,问题不大,但有时候找不到解决方法,让人纠结好久.解决方法例如以下: 编辑->配置文件首选项->常规-> monospace 改为ubuntu mon ...

  9. IP地址的规划和设计方法(二)

    五,IP地址规划方法           (1)IP地址规划的基本步骤           网络地址规划须要按下面6步进行:           a)推断用户对网络与主机数的需求:           ...

  10. Introduction to MongoDB

    https://docs.mongodb.com/getting-started/csharp/introduction/ MongoDB is an open-source document dat ...