poj3728 The merchant LCA+RMQ或 LCA+b并查集 好题!

Language:
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3020 Accepted: 999

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

Source



题意:给定n个结点构成的无向树,m个询问,起点u,终点v,在这条路径上可以买一次,卖一次,卖必须在买之后。对每次询问输出可获得的最大利润。

思路:

解法一:倍增+tarjan+RMQ

分析:先求出u,v的最近公共祖先lca,然后求出u->lca->v的利润最大值

对于这个最大值可能有3种情况:

(1)最大值是u->lca的最大值

(2)最大值是lca->v的最大值

(3)最大值是lca->v的最大值减u->lca的最小值

分析到这需要设置4个变量求最大值。

设ancestor[ i ][ j ]为结点 i 的2 ^ j祖先

设up[ i ][ j ]为由结点 i 到结点 i 的2 ^ j -1祖先获得的最大利润

设down[ i ][ j ]为由结点 i 的2 ^ j - 1 祖先到结点 i 到获得的最大利润

设dmax[ i ][ j ]为由结点 i 到结点 i 的2 ^ j - 1祖先的最大值

设dmin[ i ][ j ]为由结点 i 到结点 i 的2 ^ j - 1 祖先的最小值

令fa=ancestor[ i ][ j -1 ] , 那么我们可以得出状态转移方程:

up[ i ][ j ] = max( max( up[ i ][ j -1 ] , up[ fa ][ j - 1 ] )  , dmax[ fa ][ j - 1 ] -dmin[ i ][ j - 1 ] )

down[ i ][ j ] = max( max( down[ i ][ j -1 ] , down[ fa ][ j - 1 ] )  , dmax[ i ][ j - 1 ] -dmin[ fa ][ j - 1 ] )

dmax[ i ][ j ] = max( dmax[ i ][ j - 1 ] , dmax[ fa ][ j - 1 ] )

dmin[ i ][ j ] = min( dmin[ i ][ j - 1 ] , dmin[ fa ][ j - 1 ] )

所以先dfs预处理4个数组,然后tarjan离线查询每个询问u,v的lca,最后循环更新每个询问的最大利润,详见代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=50000+100;
int n,m,edge_cnt;
int head[MAXN],depth[MAXN],fa[MAXN],w[MAXN],vis[MAXN],lca[MAXN],a[MAXN],b[MAXN];
int dmax[MAXN][20],dmin[MAXN][20],ancestor[MAXN][20],up[MAXN][20],down[MAXN][20];
struct Edge
{
    int v;
    int next;
}edge[2*MAXN];
struct node
{
    int v;
    int id;
};
vector<node>G[MAXN];
void init()
{
    edge_cnt=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
        fa[i]=i,vis[i]=0,G[i].clear();
}
void addedge(int u,int v)
{
    edge[edge_cnt].v=v;
    edge[edge_cnt].next=head[u];
    head[u]=edge_cnt++;
}
int findset(int x)
{
    return fa[x]==x ? x:fa[x]=findset(fa[x]);
}
void dfs(int u,int f,int dep)
{
    depth[u]=dep;ancestor[u][0]=f;
    up[u][0]=down[u][0]=0;
    dmax[u][0]=dmin[u][0]=w[u];
    for(int i=1;(1<<i)<=dep;i++)
    {
        ancestor[u][i]=ancestor[ancestor[u][i-1]][i-1];
        int x=ancestor[u][i-1];
        up[u][i]=max(max(up[u][i-1],up[x][i-1]),dmax[x][i-1]-dmin[u][i-1]);
        down[u][i]=max(max(down[u][i-1],down[x][i-1]),dmax[u][i-1]-dmin[x][i-1]);
        dmax[u][i]=max(dmax[u][i-1],dmax[x][i-1]);
        dmin[u][i]=min(dmin[u][i-1],dmin[x][i-1]);
    }
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==f)
            continue;
        dfs(v,u,dep+1);
    }
}
void tarjan(int u,int f)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==f)
            continue;
        tarjan(v,u);
    }
    vis[u]=1;
    for(int i=0;i<G[u].size();i++)
    {
        node x=G[u][i];
        int v=x.v,id=x.id;
        if(!vis[v])
            continue;
        lca[id]=findset(v);
    }
    fa[u]=f;
}
int work(int x)
{
    int k=0;
    while((1<<(k+1))<=x) k++;
    return k;
}
int get_dmin(int x,int y)
{
    int i=work(depth[x]-depth[y]+1);
    if((1<<i)==depth[x]-depth[y]+1) return dmin[x][i];
    return min(dmin[x][i],get_dmin(ancestor[x][i],y));
}
int get_dmax(int x,int y)
{
    int i=work(depth[x]-depth[y]+1);
    if((1<<i)==depth[x]-depth[y]+1) return dmax[x][i];
    return max(dmax[x][i],get_dmax(ancestor[x][i],y));
}
int get_up(int x,int y)
{
    int i=work(depth[x]-depth[y]+1);
    if((1<<i)==depth[x]-depth[y]+1) return up[x][i];
    int ans=max(up[x][i],get_up(ancestor[x][i],y));
    ans=max(ans,get_dmax(ancestor[x][i],y)-dmin[x][i]);
    return ans;
}
int get_down(int x,int y)
{
    int i=work(depth[x]-depth[y]+1);
    if((1<<i)==depth[x]-depth[y]+1) return down[x][i];
    int ans=max(down[x][i],get_down(ancestor[x][i],y));
    ans=max(ans,dmax[x][i]-get_dmin(ancestor[x][i],y));
    return ans;
}
int main()
{
    freopen("text.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v); addedge(v,u);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            node x; x.v=b[i],x.id=i;
            G[a[i]].push_back(x);
            x.v=a[i];
            G[b[i]].push_back(x);
        }
        dfs(1,1,1);
        tarjan(1,1);
        for(int i=1;i<=m;i++)
        {
            int ans=max(get_up(a[i],lca[i]),get_down(b[i],lca[i]));
            ans=max(get_dmax(b[i],lca[i])-get_dmin(a[i],lca[i]),ans);
            printf("%d\n",ans);
        }
    }
    return 0;
}

解法二: tarjan+并查集

设up[ u ]为 u->lca的利润最大值

设down[ u ]为lca->u的利润最大值

设dmax[ u ]为u-lca之间的最大w[ i ]

设dmin[ u ]为u-lca之间的最小w[ i ]

利用种类并查集的思想,更新这4个变量。

在tarjan求lca的过程中,对于(u , v )的 lca , 设为f , 建一个vector存储以该f 为 lca 的询问 id,当回溯到 f 时,从vector中读取询问的id , 获取路径起点u,终点v 。findset更新u,v,最后输出每个询问对应的最大利润值,详见代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXN=50000+100;
int n,m,edge_cnt;
int head[MAXN],vis[MAXN],fa[MAXN],a[MAXN],b[MAXN],w[MAXN],sum[MAXN];
int dmax[MAXN],dmin[MAXN],up[MAXN],down[MAXN];
struct Edge
{
    int v;
    int next;
}edge[2*MAXN];
struct node
{
    int id;
    int u,v;
};
vector<node>G[MAXN],mp[MAXN];

void init()
{
    edge_cnt=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
        vis[i]=0,fa[i]=i,G[i].clear(),mp[i].clear();
}
void addedge(int u,int v)
{
    edge[edge_cnt].v=v;
    edge[edge_cnt].next=head[u];
    head[u]=edge_cnt++;
}
int findset(int x)
{
    if(fa[x]!=x)
    {
        int root=fa[x];
        fa[x]=findset(fa[x]);
        up[x]=max(max(up[x],up[root]),dmax[root]-dmin[x]);
        down[x]=max(max(down[x],down[root]),dmax[x]-dmin[root]);
        dmax[x]=max(dmax[x],dmax[root]);
        dmin[x]=min(dmin[x],dmin[root]);
    }
    return fa[x];
}
void tarjan(int u,int f)
{
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(v==f)
            continue;
        tarjan(v,u);
    }
    vis[u]=1;
    for(int i=0;i<G[u].size();i++)
    {
        node x=G[u][i];
        int v=x.v,id=x.id;
        if(!vis[v])
            continue;
        int lca=findset(v);
        mp[lca].push_back(x);
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int id=mp[u][i].id;
        int x=a[id],y=b[id];
        findset(x); findset(y);
        sum[id]=max(max(up[x],down[y]),dmax[y]-dmin[x]);
    }

    fa[u]=f;
}
int main()
{
    //freopen("text.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1;i<=n;i++)
        {
             scanf("%d",&w[i]);
             up[i]=down[i]=0;
             dmax[i]=dmin[i]=w[i];
        }
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v); addedge(v,u);
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a[i],&b[i]);
            node x; x.v=b[i]; x.id=i;
            G[a[i]].push_back(x);
            x.v=a[i];
            G[b[i]].push_back(x);
        }
        tarjan(1,1);
        for(int i=1;i<=m;i++)
            printf("%d\n",sum[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值