POJ——T 3728 The merchant
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 ≤ N, wi, Q ≤ 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
#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的更多相关文章
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
- poj 3728 The merchant(LCA)
Description There are N cities in a country, and there is one and only one simple path between each ...
- POJ 3728 The merchant(并查集+DFS)
[题目链接] http://poj.org/problem?id=3728 [题目大意] 给出一棵树,每个点上都可以交易货物,现在给出某货物在不同点的价格, 问从u到v的路程中,只允许做一次买入和一次 ...
- poj——3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5055 Accepted: 1740 Desc ...
- [POJ 3728]The merchant
Description There are N cities in a country, and there is one and only one simple path between each ...
- 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 倍增lca求dp
题目: zdf给出的题目翻译: 从前有一个富饶的国度,在这里人们可以进行自由的交易.这个国度形成一个n个点的无向图,每个点表示一个城市,并且有一个权值w[i],表示这个城市出售或收购这个权值的物品.又 ...
- POJ 3728 The merchant (树形DP+LCA)
题目:https://vjudge.net/contest/323605#problem/E 题意:一棵n个点的树,然后有m个查询,每次查询找(u->v)路径上的两个数,a[i],a[j],(i ...
- POJ 3278:The merchant(LCA&DP)
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6864 Accepted: 2375 Desc ...
随机推荐
- 【转载】java编程中'为了性能'一些尽量做到的地方
1.尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面 第一,控制资源的使用,通过线程同步来控制资 ...
- python下载网页转化成pdf
最近在学习一个网站补充一下cg基础.但是前几天网站突然访问不了了,同学推荐了waybackmachine这个网站,它定期的对网络上的页面进行缓存,但是好多图片刷不出来,很憋屈.于是网站恢复访问后决定把 ...
- HDU 1548 A strange lift【BFS】
题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直 ...
- ActiveMQ学习笔记(9)----ActiveMQ静态网络连接
1. 启动多个Broker 在win10下同一台服务器启动多个Broker, 步骤如下: 1. 复制安装目录下的conf文件夹命名为conf2 2. 修改activemq.xml中的brokerNam ...
- 织梦dedecms支持flash的flv文件播放功能代码
1.打开/include/FCKeditor/editor/dialog/dede_media.htm if(playtype=="rm"|| (playtype=="- ...
- SSIS安装以及安装好找不到商业智能各种坑
原文:SSIS安装以及安装好找不到商业智能各种坑 这两天为了安装SSIS,各种头疼.记录一下,分享给同样遇到坑的.. 安装SSIS需要几个步骤. 先说一下我的情况,安装SQL的时候,一直默认下一步,没 ...
- 经典C语言编程注意点
C/C++程序员应聘试题剖析 分中的2分.读者可从本文看到strcpy函数从2分到10分解答的例子,看看自己属于什么样的层次.此外,还有一些面试题考查面试者敏捷的思维能力. 分析这些面试题,本身包含很 ...
- Linux系统信息查看命令大全[转]
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinf ...
- 调用Windows属性窗口
简述 在Windows系统下.可以通过:右键 -> 属性,来查看文件/文件夹对应的属性信息,包括:常规.安全.详细信息等. 简述 共有类型 共有类型 首先,需要包含头文件: #include & ...
- hdu_2871
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...