The merchant(LCA——Tarjan高级应用)

http://poj.org/problem?id=3728

The merchant
Time Limit: 3000MS
Memory Limit: 65536K
Total Submissions: 4117
Accepted: 1387

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

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个点,n-1条边),每个点都有相应的权值,问你从你从一个点买进它的权值,再从另一个点卖出(买卖只能有一次),可以获得的最大利益是多少,不能走回头路。

     也就是可以转化为,从一个点u出发,你所能走到的最高位置就是u和v的最近公共祖先节点(比喻为山顶,那么任何一次从u到v都可以比作一次上山和下山过程),那么,你的获益有三种情况:1.在“上山”途中已经进行了买卖2.在上山的时候买进,在下山的时候卖出3.在下山的途中进行买卖。。。你的任务就是比较这三种情况的最大值。

     于是就有了up[ ](上山途中的最大收益),down[ ](下山途中的最大收益),mx[u]从u到山顶的最大节点,mi[u]从u到山顶的最小节点。。也就是说,上面的三种情况可以分别表示为 1.up[u] 2. mx[v] - mi[u] 3. down[v]

     这个题还是Tarjan的模板,里面加进去了一些小内容,其中,在执行find函数的时候,在寻找祖先节点,应该说是找到了“山顶”,在回溯的过程中,对up down mx 和 mi进行了更新,四句话的顺序也是很有讲究,可以看着代码好好理解一下

<span style="font-size:14px;">#include <cstdio>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
int const MAX = 50005;
int n, tot;
int pre[MAX];
int vis[MAX];
int up[MAX], down[MAX], mx[MAX], mi[MAX];
vector<int> G[MAX], ask[MAX], pos[MAX];
vector<int> a[MAX], b[MAX], id[MAX];
int ans[MAX];
int find(int x)
{
    if(x == pre[x])     return x;
    int y = pre[x];
    pre[x] = find(y);
    //下面为更新内容,需要好好理解一下这个过程
    up[x] = max(up[x], max((mx[y] - mi[x]), up[y]));
    down[x] = max(down[x], max((mx[x] - mi[y]), down[y]));
    mx[x] = max(mx[x], mx[y]);
    mi[x] = min(mi[x], mi[y]);
    return pre[x];
}

void tarjan(int u)
{
    vis[u] = 1;
    for(int i = 0; i < ask[u].size(); i++){//回溯
        int v = ask[u][i];
        if(vis[v]){//如果v已经被找过了,那么find(v)就是u和v的共同最近祖先节点
            int t = find(v);//找出它们的最近祖先节点t
            int z = pos[u][i];
            if(z > 0){//如果z为正那么问题就是从u到v,询问id就是z
                //可以看作三个并列的容器,下面三句话储存的信息是:从u到v他们的最近公共祖先是t,他们被询问的id是z(z也就是被第几次询问)
                a[t].push_back(u);
                b[t].push_back(v);
                id[t].push_back(z);
            }
            else{//如果z为负,那么应该把u和v倒过来,因为一开始放置的时候就已经把u和v倒了,询问id是-z
                //下面三句话也是同理
                a[t].push_back(v);
                b[t].push_back(u);
                id[t].push_back(-z);
            }
        }
    }
    for(int i = 0; i < G[u].size(); i++){//主干
        int v = G[u][i];
        if(!vis[v]){
            tarjan(v);
            pre[v] = u;//这个也是回溯上来的时候形成的
        }
    }
    for(int i = 0; i < a[u].size(); i++){
        int x = a[u][i];//x为起点
        int y = b[u][i];//y为终点
        int t = id[u][i];//t为id
        find(x);//及时更新
        find(y);//及时更新
        ans[t] = max(up[x], max(down[y], mx[y] - mi[x]));//最终的ans就是比较三种情况的大小
    }
}

int main()
{
    int u, v, w;
    memset(up, 0, sizeof(up));
    memset(down, 0, sizeof(down));
    memset(vis, 0, sizeof(vis));
    tot = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &w);
        pre[i] = i;
        mx[i] = mi[i] = w;
    }
    for(int i = 1; i < n; i++){
        scanf("%d %d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int q;
    scanf("%d", &q);
    for(int i = 1; i <= q; i++){
         scanf("%d %d", &u, &v);
         ask[u].push_back(v);
         pos[u].push_back(i);
         ask[v].push_back(u);
         pos[v].push_back(-i);
    }
    tarjan(1);
    for(int i = 1; i <= q; i++)
        printf("%d\n", ans[i]);
    return 0;
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值