POJ 3728 The merchant(经典LCA)

本文介绍了一种使用LCA算法求解在特定路径上能够获取的最大利润问题的方法。通过构建稀疏表格来预处理节点间的最大和最小价格,进而快速计算任意两点间路径上的最大收益。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The merchant
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 ≤ 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题目,除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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值