The merchant - POJ 3728 LCA

The merchant
Time Limit: 3000MS Memory Limit: 65536K
Total 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

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中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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值