LCA+并查集应用(好题)poj3728

来源:互联网 发布:图标设计图案软件 编辑:程序博客网 时间:2024/04/30 00:56

Language:
The merchant
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 3143 Accepted: 1047

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

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

Sample Output

422000020
题意:给出一棵节点有值的树,给出Q个询问(a,b),问从a到b的最大盈利(即:先在最小值买入,再在最大值卖出)

思路:LCA离线算法,维护四个值,up,down,minv,maxv,详见代码

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=50010;vector<int> g[maxn];vector<pair<int,int> > qu[maxn];vector<pair<int,int> > ans[maxn];//保存该节点作为lca,它的子节点中都有哪些查询int res[maxn];int n,q,val[maxn],pre[maxn],vis[maxn];//up表示从当前节点到当前lca的最大盈利,down表示从从当前lca到当前节点的最大盈利,maxv表示节点到lca的最大值,minv表示最小值//这样查询u->v,就是比较up[u],down[v]和maxv[v]-minv[u]的最大值int up[maxn],down[maxn],maxv[maxn],minv[maxn];int find(int x){    if(x==pre[x])return x;;    int fa=pre[x];    pre[x]=find(pre[x]);    up[x]=max(up[x],max(up[fa],maxv[fa]-minv[x]));//刚开始想求最大值和最小值得点的顺序不一定符合要求,为什么要这样更新,后来发现minv和maxv初始值就是本节点的值    down[x]=max(down[x],max(down[fa],maxv[x]-minv[fa]));    maxv[x]=max(maxv[x],maxv[fa]);    minv[x]=min(minv[x],minv[fa]);    return pre[x];}void LCA(int u){    int len=qu[u].size();    for(int i=0;i<len;i++)    {        int v=qu[u][i].second;        if(vis[v])        {            int lca=find(v);            ans[lca].push_back(make_pair(u,i));//把询问添加到lca,这样当该LCA节点的所有子节点访问完之后,就可以一起更新答案了        }    }    vis[u]=1;    pre[u]=u;    len=g[u].size();    for(int i=0;i<len;i++)    {        int v=g[u][i];        if(vis[v])continue;        LCA(v);        pre[v]=u;    }    len=ans[u].size();    for(int i=0;i<len;i++)    {        int x=ans[u][i].first,y=qu[x][ans[u][i].second].second;        int id=qu[x][ans[u][i].second].first;        if(id<0){id=-id;swap(x,y);}//保证顺序是对的        find(x);find(y);        res[id]=max(up[y],down[x]);        res[id]=max(res[id],maxv[x]-minv[y]);    }}int main(){    scanf("%d",&n);    for(int i=1;i<=n;i++)scanf("%d",&val[i]);    memset(up,0,sizeof(up));    memset(down,0,sizeof(down));    for(int i=1;i<=n;i++)        maxv[i]=minv[i]=val[i];    for(int i=1;i<n;i++)    {        int u,v;        scanf("%d%d",&u,&v);        g[u].push_back(v);        g[v].push_back(u);    }    scanf("%d",&q);    for(int i=1;i<=q;i++)    {        int u,v;        scanf("%d%d",&u,&v);        qu[u].push_back(make_pair(-i,v));        qu[v].push_back(make_pair(i,u));    }    memset(vis,0,sizeof(vis));    LCA(1);    for(int i=1;i<=q;i++)printf("%d\n",res[i]);    return 0;}




0 0