poj——3728 The merchant

                          The merchant

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5055   Accepted: 1740

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

POJ Monthly Contest – 2009.04.05, GaoYihan

题目大意:给一棵N个点的树,每个点上都买东西,给你每个点上的价格,现在有一个商人,当他从一个点出发走到另一个点的时候(因为是树,路径唯一),在路上有一次机会在一个点上买一个东西,而在后面的一个点上卖掉来挣钱,当然他也可以不要这一次机会。现在的问题是,给出M个询问,每个询问一个起点,一个终点,你来回答路上最多能挣多少钱。

思路:(天哪,这是我见过最恶心的lca!没有之一!!!)

我们维护4个值,一是到当前点的最大值,二是到当前点的最小值,三是从上往下(从父亲到儿子)的最大利润,四是从下往上(从儿子到父亲)的最大利润。

然后我们进行倍增更新这四个值。由于我们是一棵树,所以我们可以用当前点的儿子和他本身来更新这几个值

也就是这样:  shop[x][i].maxx=max(shop[x][i-1].maxx,shop[shop[x][i-1].dad][i-1].maxx);
        shop[x][i].minn=min(shop[x][i-1].minn,shop[shop[x][i-1].dad][i-1].minn);

有人会问不是说要用他自己和他的儿子来更新吗?!那shop[shop[x][i-1].dad][i-1].minn这又是个什么鬼?!

这个地方我也是纠结了好长时间,我们更新的时候有一句话是这样的:shop[x][i].dad=shop[shop[x][i-1].dad][i-1].dad;这样的话也就是说这两个点是相等的。为什么?!我们来看shop[x][i].dad这里是x的第2^i个父亲,后面的是它的第2^(i-1)的第2^(i-1)个父亲,我们可以代入数据试一下,这两个值是相等的。例如:当i=1的时候她的第二个父亲不就是它的父亲的父亲吗。。。当i=2的时候,2^2=(2^1)+(2^1)  当i=3时,2^3=(2^2)+(2^2)   当i=4 2^4=2^3+2^3………………这样我们就可以很好地理解为什么会是这样了吧、、、是不是又在想那直接更新不就行了,让过来绕过去不还是用它自己更新吗、、、可是我们知道我们是挨着更新的,i-1是更新过的但是i就不保证是更新过的了。。。。

然后我们再求lca,为什么要求lca?!

啊啊啊,你去一个地方肯定是走最近的路了,那不就是一定会走lca吗、、、(除非你闲的没事干、、、)

哼,有位大佬跟我说,求lca是因为他没法倒着走,如果走到比LCA深度还深的点,就到不了目的地了、、、、、、

所以我们的路径分为两种情况一个是往上走的另一个是往下走的,我们给出的数据保证第一个数是往上走,第二个数可能是lca也可能往下走。

这样我们就可以明白为什么我们要记录从上往下(从父亲到儿子)的最大利润,从下往上(从儿子到父亲)的最大利润了。

我们在进行得到结果时会有这三种买卖的情况,一是我们在x到lca这进行交易完毕,二是我们在lca到y才进行交易,三是我们在x到lca处买入在lca到y处卖出、、、所以我们对于每一组询问,更新从x到lca的最小值,更新从lca到y的最大值,然后在最后经行比较,同样我们需要更新从上到下可获得最大利润以及从下到上可获得最小利润,这个地方我们采用倍增。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 200010
#define maxn 9999999
using namespace std;
int n,m,x,y,tot,v[N],head[N],deep[N];
struct Edge
{
    int to,from,next;
}edge[N<<1];
struct Shop
{
    int dad,minn,maxx,up_down,down_up;
}shop[N][25];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();}
    return x*f;
}
int dfs(int x,int fa)
{
    deep[x]=deep[fa]+1;
    shop[x][0].maxx=max(v[x],v[fa]);
    shop[x][0].minn=min(v[x],v[fa]);
    shop[x][0].up_down=max(0,v[x]-v[fa]);
    shop[x][0].down_up=max(0,v[fa]-v[x]);
    for(int i=1;shop[x][i-1].dad;i++)
    {
        shop[x][i].dad=shop[shop[x][i-1].dad][i-1].dad;
        shop[x][i].maxx=max(shop[x][i-1].maxx,shop[shop[x][i-1].dad][i-1].maxx);
        shop[x][i].minn=min(shop[x][i-1].minn,shop[shop[x][i-1].dad][i-1].minn);
        shop[x][i].down_up=max(shop[x][i-1].down_up,shop[shop[x][i-1].dad][i-1].down_up);
        shop[x][i].down_up=max(shop[x][i].down_up,shop[shop[x][i-1].dad][i-1].maxx-shop[x][i-1].minn);
        shop[x][i].up_down=max(shop[x][i-1].up_down,shop[shop[x][i-1].dad][i-1].up_down);
        shop[x][i].up_down=max(shop[x][i].up_down,shop[x][i-1].maxx-shop[shop[x][i-1].dad][i-1].minn);
    }
    for(int i=head[x];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(shop[x][0].dad!=t)
         shop[t][0].dad=x,dfs(t,x);
    }
}
int Lca(int x,int y)
{
    if(deep[x]>deep[y]) swap(x,y);
    for(int i=20;i>=0;i--)
     if(deep[shop[y][i].dad]>=deep[x])
      y=shop[y][i].dad;
    if(x==y) return x;
    for(int i=20;i>=0;i--)
     if(shop[x][i].dad!=shop[y][i].dad)
      x=shop[x][i].dad,y=shop[y][i].dad;
    return shop[x][0].dad;
}
int ask(int x,int y)
{
    int ans=0,maxx=-maxn,minn=maxn,lca=Lca(x,y);
    for(int i=20;i>=0;i--)
     if(deep[shop[x][i].dad]>=deep[lca])
       {
             ans=max(ans,max(shop[x][i].down_up,shop[x][i].maxx-minn));
             minn=min(shop[x][i].minn,minn);
             x=shop[x][i].dad;
      }
    for(int i=20;i>=0;i--)
     if(deep[shop[y][i].dad]>=deep[lca])
      {
          ans=max(ans,max(shop[y][i].up_down,maxx-shop[y][i].minn));
          maxx=max(shop[y][i].maxx,maxx);
          y=shop[y][i].dad;
      }
    return max(ans,maxx-minn);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++) v[i]=read();
    for(int i=1;i<n;i++) x=read(),y=read(),add(x,y),add(y,x);
    dfs(1,0);
    m=read();
    for(int i=1;i<=m;i++)
    {
        x=read(),y=read();
        if(x==y) printf("0\n");
        printf("%d\n",ask(x,y));
    }
    return 0;
}

mdzz这个题居然因为一个数组然后调了一下午?!

什么嘛,倍增的时候倍增的数一定要比数组的第二维小!!!

时间: 2024-08-08 09:29:26

poj——3728 The merchant的相关文章

[最近公共祖先] POJ 3728 The merchant

The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 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 w

poj 3728 The merchant(LCA)

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 on

POJ - 3728 The merchant(dp+LCA)

题目大意:给出N个点,和每个点物品的售价,现在有一个商人,要从u点到v点,他想在路上多赚点钱.他可以从一个城市买物品,然后再卖到另一个城市,但买卖只允许一次,且不能回头走 问最多能赚多少 解题思路:果然智商捉急了.. up数组纪录当前点到lca的最大利润 down数组纪录lca到当前点的最大利润 Max数组lca到当前点的最大值 Min纪录当前点到lca的最小值 这样的话,执行tarjan的时候,就可以更新一下这些值了 首先,答案的话肯定是max(max(up, down), Max - Min

POJ - 3728:The merchant (Tarjan 带权并查集)

题意:给定一个N个节点的树,1<=N<=50000 每个节点都有一个权值,代表商品在这个节点的价格.商人从某个节点a移动到节点b,且只能购买并出售一次商品,问最多可以产生多大的利润. 思路:路径压缩,得到每个点到当前根的信息,然后更新即可. 有可以用倍增做. 很久前抄的代码. #include<cstdio> #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) #define swap(a,b) (a^=b,b^=

POJ 3728

题意: 一个树形图,有个二货商人,旅游时候还想着赚钱!从某个地方到另一个地方时,可以旅途中进一批货(应该人手不够,手里只能拿一批),然后在旅途中卖掉,求最大能赚多少钱. 思路: 赤裸裸的LCA,ans(x,y)=max(up(x,lca),down(lca,y),maxp(lca,y)-min(lca,x)); 注意每次更新要在父亲节点处更新,繁琐了些.还是多用用STL吧! 竟然犯了一个无语的RE,忘了是双向边.去切腹吧  — .— 代码: #include <cstdio> #include

POJ 3728 离线 LCA

题意很简单 给一个树(n < 5w) 每个点有个权值,代表商品价格 若干个询问(5w) 对每个询问,问的是从u点走到v点(简单路径),商人在这个路径中的某点买入商品,然后在某点再卖出商品,   最大可能是多少 注意一条路径上只能买卖一次,先买才能卖 用的方法是离线LCA,在上面加了一些东西 对于一个询问, 假设u,v的LCA是f 那么有三种可能, 一个是从u到f 买卖了. 一个是从f到v买卖了,  一个是从u到f之间买了,从v到f卖了 从u到f 我们称为up,  从f到v我们称为down,而从u

POJ 3728 Catch That Cow

Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小