The merchant - POJ 3728 LCA

来源:互联网 发布:矩阵形阵 编辑:程序博客网 时间:2024/05/21 21:48

The merchant
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 3177 Accepted: 1061

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

41 5 3 21 33 23 491 21 31 42 32 12 43 13 23 4

Sample Output

422000020

题意:在一个路径上,你可以选择一个在一个点买东西,然后在其后面的点去卖东西,问最大的差价是多少。

思路:LCA中parent[k][v]可以表示v的2^k的祖先节点,同样,我们也可以这么表示v到v的2^k的祖先节点中的最大数和最小数,然后假设c是a,b的祖节点,那么ans=max(up(a,c),down(c,a),MAX(c,b)-MIN(c,a))

AC代码如下:

#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;vector<int> G[50010];int parent[30][50010],depth[50010],minn[30][50010],maxn[30][50010],pow2[30],vis[50010],val[50010],ans;void dfs(int v,int p,int d){ parent[0][v]=p;  depth[v]=d;  if(p)  { minn[0][v]=min(val[v],val[p]);    maxn[0][v]=max(val[v],val[p]);  }  else  { minn[0][v]=val[v];    maxn[0][v]=val[v];  }  vis[v]=1;  int i,len=G[v].size();  for(i=0;i<len;i++)   if(vis[G[v][i]]==0)    dfs(G[v][i],v,d+1);}void init(int V){ dfs(1,-1,0);  int k,v;  for(k=0;k+1<30;k++)   for(v=1;v<=V;v++)    if(parent[k][v]<0)     parent[k+1][v]=-1;    else    { parent[k+1][v]=parent[k][parent[k][v]];      minn[k+1][v]=min(minn[k][v],minn[k][parent[k][v]]);      maxn[k+1][v]=max(maxn[k][v],maxn[k][parent[k][v]]);    }}int lca(int u,int v){ if(depth[u]>depth[v])   swap(u,v);  int k;  for(k=0;k<30;k++)   if((depth[v]-depth[u])>>k &1)    v=parent[k][v];  if(u==v)   return u;  for(k=29;k>=0;k--)   if(parent[k][u]!=parent[k][v])   { u=parent[k][u];     v=parent[k][v];   }  return parent[0][u];}int MAX(int u,int v){ if(u==v)   return val[u];  int len=depth[v]-depth[u],k=0;  while(pow2[k]<=len)   k++;  k--;  return max(maxn[k][v],MAX(u,parent[k][v]));}int MIN(int u,int v){ if(u==v)   return val[u];  int len=depth[v]-depth[u],k=0;  while(pow2[k]<=len)   k++;  k--;  return min(minn[k][v],MIN(u,parent[k][v]));}void up(int a,int b){ if(MAX(b,a)-MIN(b,a)<=ans)   return;  if(parent[0][a]==b)  { ans=max(ans,val[b]-val[a]);    return;  }  int len=depth[a]-depth[b],k=0,c;  while(pow2[k]*2<=len)   k++;  k--;  c=parent[k][a];  ans=max(ans,MAX(b,c)-MIN(c,a));  up(c,b);  up(a,c);}void down(int a,int b){ if(MAX(a,b)-MIN(a,b)<=ans)   return;  if(parent[0][b]==a)  { ans=max(ans,val[b]-val[a]);    return;  }  int len=depth[b]-depth[a],k=0,c;  while(pow2[k]*2<=len)   k++;  k--;  c=parent[k][b];  ans=max(ans,MAX(c,b)-MIN(a,c));  down(a,c);  down(c,b);}int main(){ int n,m,i,j,k,u,v,a,b,c;  for(i=0;i<=30;i++)   pow2[i]=1<<i;  scanf("%d",&n);  for(i=1;i<=n;i++)   scanf("%d",&val[i]);  for(i=2;i<=n;i++)  { scanf("%d%d",&u,&v);    G[u].push_back(v);    G[v].push_back(u);  }  init(n);  scanf("%d",&m);  while(m--)  { scanf("%d%d",&a,&b);    ans=0;    c=lca(a,b);    if(c!=a && c!=b)    { ans=MAX(c,b)-MIN(c,a);      up(a,c);      down(c,b);    }    else if(a==c)     down(a,b);    else     up(a,b);    printf("%d\n",ans);  }}



0 0
原创粉丝点击