Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions:5599 | Accepted: 1931 |
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
题解:
经典LCA题目,除f数组外还需要MAX[x][h]从x点到f[x][h]所经过点的
最大值,MIN同,p1[x][h]则表示从x点走到f[x][h]点所能获得的最大利益。
p2[x][h]表示从f[x][h]走到x所能获得的最大利益
代码:
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=50007;
int dep[maxn],lg[maxn],f[maxn][33],val[maxn],MAX[maxn][33],MIN[maxn][33];
int n,Q,head[maxn],tol,p1[maxn][33],p2[maxn][33];
bool vis[maxn];
struct node
{
int to,next;
} rode[maxn*4];
void add(int a,int b)
{
rode[tol].to=b;
rode[tol].next=head[a];
head[a]=tol++;
}
void init()
{
for(int i=2; i<maxn; i++)
lg[i]=lg[i-1]+(1<<lg[i-1]+1==i);
}
void dfs(int u,int fa)
{
dep[u]=dep[fa]+1;
f[u][0]=fa;
MAX[u][0]=max(val[u],val[fa]);
MIN[u][0]=min(val[u],val[fa]);
p1[u][0]=max(0,val[fa]-val[u]);
p2[u][0]=max(0,val[u]-val[fa]);
for(int i=1; (1<<i)<=dep[u]; i++)
{
f[u][i]=f[f[u][i-1]][i-1];
MAX[u][i]=max(MAX[u][i-1],MAX[f[u][i-1]][i-1]);
MIN[u][i]=min(MIN[u][i-1],MIN[f[u][i-1]][i-1]);
p1[u][i]=max(p1[u][i-1],p1[f[u][i-1]][i-1]);
p1[u][i]=max(p1[u][i],MAX[f[u][i-1]][i-1]-MIN[u][i-1]);
p2[u][i]=max(p2[u][i-1],p2[f[u][i-1]][i-1]);
p2[u][i]=max(p2[u][i],MAX[u][i-1]-MIN[f[u][i-1]][i-1]);
}
for(int i=head[u]; i!=-1; i=rode[i].next)
if(rode[i].to!=fa)
dfs(rode[i].to,u);
}
int lca(int x,int y)
{
if(dep[x]<dep[y])
swap(x,y);
while(dep[x]>dep[y])
{
int h=lg[dep[x]-dep[y]];
x=f[x][h];
}
if(x==y) return x;
for(int i=lg[dep[x]]; i>=0; i--)
{
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
}
return f[x][0];
}
int main()
{
init();
while(~scanf("%d",&n))
{
for(int i=1; i<=n; i++)
scanf("%d",&val[i]);
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(MAX,0,sizeof(MAX));
memset(MIN,0,sizeof(MIN));
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
tol=1;
for(int i=1; i<n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(1,0);
scanf("%d",&Q);
while(Q--)
{
int x,y;
scanf("%d%d",&x,&y);
int t=lca(x,y);
int valm=0,ma=0,mi=1<<30;
while(dep[x]>dep[t])
{
int h=lg[dep[x]-dep[t]];
valm=max(valm,p1[x][h]);
valm=max(valm,MAX[x][h]-mi);
mi=min(mi,MIN[x][h]);
x=f[x][h];
}
while(dep[y]>dep[t])
{
int h=lg[dep[y]-dep[t]];
valm=max(valm,p2[y][h]);
valm=max(valm,ma-MIN[y][h]);
ma=max(ma,MAX[y][h]);
y=f[y][h];
}
printf("%d\n",max(ma-mi,valm));
}
}
return 0;
}