poj 3728 merchant 一个比较复杂的lca+dp

8 篇文章 0 订阅

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
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

题意很简单

给一个树(n < 5w) 每个点有个权值,代表商品价格

若干个询问(5w)

对每个询问,问的是从u点走到v点(简单路径),商人在这个路径中的某点买入商品,然后在某点再卖出商品, 最大可能是多少

注意一条路径上只能买卖一次,先买才能卖

/*分析:先求出点u,v的最近公共祖先f,然后求u->f->v的利润最大值maxval
对于这个maxval可能有三种情况:
1:maxval是u->f的maxval
2:maxval是f->v的maxval
3:maxval是u->f的最小w[i]减去f->v的最大w[i]
分析到这很明显需要设置4个变量来求maxval:
up[u]表示u->f的最大maxval
down[u]表示f->u的最大maxval
maxw[u]表示u-f的最大w[i]
minw[u]表示u-f的最小w[i]
所以maxval=max(max(up[u],down[v]),maxw[v]-minw[u]);
现在问题就是如何快速的求出这四个变量,在这里我们可以对u,v的LCA(u,v)进行分类解决
对于LCA(u,v)是f的询问全部求出,然后再求LCA(u,v)是f的父亲的询问
这样当我们求LCA(u,v)是f的父亲的询问的时候就可以借用已经求出的LCA(u,v)是f的询问
的结果,这样就不用反复去求u->f的那四个变量值,u->father[f]也能快速求出
这个变化主要在寻找father[v]这个过程中进行,具体看代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int n;
const int INF=0x3f3f3f3f;
const int maxn=5e4+10;
int vis[maxn];
int deep[maxn];
vector<int> v[maxn];
struct Node
{
    int mi,ma,num1,num2,p;
    Node(){
        mi=INF;
        ma=-INF;
        num1=0,num2=0;
        p=-1;
    }
    void update(const Node &a,const Node &b)
    {
        this->num1=max(a.num1,b.num1);
        this->num1=max(this->num1,b.ma-a.mi);

        this->num2=max(a.num2,b.num2);
        this->num2=max(this->num2,a.ma-b.mi);

        this->mi=min(a.mi,b.mi);
        this->ma=max(a.ma,b.ma);
        this->p=b.p;
    }

    Node(int _mi,int _ma,int _num1,int _num2,int _p):mi(_mi),ma(_ma),num1(_num1),num2(_num2),p(_p){}
}node[maxn],dp[maxn][30];

void dfs(int x)
{
    for(int i=0;i<v[x].size();i++)
    {
        int vv=v[x][i];
        if(deep[vv]) continue;
        deep[vv]=deep[x]+1;
        dp[vv][0].update(node[vv],node[x]);
        dfs(vv);
    }
}

int lca(int u,int v)
{
    Node ansu,ansv;
    int du=0,dv=0;
    for(dv=0;(1<<(dv+1))<=deep[v];dv++);
    for(du=0;(1<<(du+1))<=deep[u];du++);
    if(deep[u]>deep[v])
    {
        for(int i=du;i>=0;i--)
        {
            if(deep[u]-deep[v]>=(1<<i))
            {
                ansu.update(ansu,dp[u][i]);
                u=dp[u][i].p;
            }
        }
        if(u==v) return ansu.num1;
    }
    else if(deep[u]<deep[v])
    {
        for(int i=dv;i>=0;i--)
        {
            if(deep[v]-deep[u]>=(1<<i))
            {
                ansv.update(ansv,dp[v][i]);
                v=dp[v][i].p;
            }
        }
        if(u==v) return ansv.num2;
    }
    int ans=0;
    for(int i=du;i>=0;i--)
    {
        if(dp[u][i].p!=-1&&dp[u][i].p!=dp[v][i].p)
        {
            ansu.update(ansu,dp[u][i]);
            ansv.update(ansv,dp[v][i]);
            u=dp[u][i].p;
            v=dp[v][i].p;
        }
    }
    ansu.update(ansu,dp[u][0]);
    ansv.update(ansv,dp[v][0]);
    ans=max(ansu.num1,ansv.num2);
    ans=max(ans,ansv.ma-ansu.mi);
    return ans;
}

void ST()
{
    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=1;i<=n;i++)
        {
            if(dp[i][j-1].p!=-1)
            {
                dp[i][j].update(dp[i][j-1],dp[dp[i][j-1].p][j-1]);

            }
        }
    }
}


int main()
{
    cin>>n;
    memset(vis,0,sizeof(vis));
    memset(deep,0,sizeof(deep));
    for(int i=1;i<=n;i++)
    {
        int d;
        scanf("%d",&d);
        node[i].mi=node[i].ma=d;
        node[i].num1=node[i].num2=0;
        node[i].p=i;
    }
    for(int i=0;i<n-1;i++)
    {
        int c,d;
        scanf("%d%d",&c,&d);
        v[c].push_back(d);
        vis[d]++;
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            dfs(i);
            break;
        }
    }
    // dfs(1);
    // for(int i=1;i<=n;i++)
    //  printf("%d ",deep[i] );
    // printf("\n");
    // for(int i=1;i<=n;i++)
    // {
    //  printf("%d %d %d %d %d\n",dp[i][0].p,dp[i][0].num1,dp[i][0].num2,dp[i][0].mi,dp[i][0].ma );
    // }
    ST();
    int m;
    scanf("%d",&m);
    for(int i=0;i<m;i++)
    {
        int c,d;
        scanf("%d%d",&c,&d);
        printf("%d\n",lca(c,d) );
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值