LCA 练习题:【POJ3728】The merchant

本文介绍了一种使用树形DP算法解决路径最大收益问题的方法。通过预先处理树的结构,利用RMQ技术和LCA算法,快速计算任意两点间路径上的最大盈利。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int MAXN = 500010,  INF = 233333333;

struct Edge
{
	int v, id, next;
	Edge(){}
	Edge(int V, int ID, int NEXT) : v(V), id(ID), next(NEXT){}
};

int n, m, size, top;
int uu[MAXN], vv[MAXN], ww[MAXN], anc[MAXN];
int up[MAXN][20], down[MAXN][20], maxw[MAXN][20], minw[MAXN][20], deep[MAXN];
int head[MAXN], head2[MAXN], bin[MAXN], stack[MAXN], mp[MAXN][20], father[MAXN];
bool mark[MAXN];

Edge edge[MAXN * 2], edge2[MAXN * 2];

void Init(int num)
{
	for (int i = 0; i <= num; ++ i) head[i] = head2[i] =  - 1, mark[i] = false;
	size = top = 0;
}

void Insert(int u, int v, int id)
{
	edge[size] = Edge(v, id, head[u]);
	head[u] = size ++ ;
}

void Insert2(int u, int v, int id)
{
	edge2[size] = Edge(v, id, head2[u]);
	head2[u] = size ++ ;
}

void dfs(int u, int father, int k)
{
	deep[u] = k;
	for (int i = head[u]; i != - 1; i = edge[i].next){
		int v = edge[i].v;
		if (v == father) continue;
		dfs(v, u, k + 1);
	}
}

void RMQ(int u, int father)
{
	stack[ ++ top] = u;
	int fa = stack[top - 1];
	up[u][0] = down[u][0] = 0;
	maxw[u][0] = minw[u][0] = ww[u];
	for (int i = 1;bin[i] <= top; ++ i)
    {
		fa = stack[top - bin[i - 1]];
		up[u][i] = max(max(up[u][i - 1], up[fa][i - 1]), maxw[fa][i - 1] - minw[u][i - 1]);
		down[u][i] = max(max(down[u][i - 1], down[fa][i - 1]), maxw[u][i - 1] - minw[fa][i - 1]);
		maxw[u][i] = max(maxw[u][i - 1], maxw[fa][i - 1]);
		minw[u][i] = min(minw[u][i - 1], minw[fa][i - 1]);
		mp[u][i] = stack[top - bin[i]];
	}
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].v;
		if (v == father) continue;
		RMQ(v, u);
	}
	--top;
}

int findset(int v)
{
	if (v !=  father[v]) father[v] = findset(father[v]);
	return father[v];
}

void LCA(int u)
{
	mark[u] = true;
	father[u] = u;
	for (int i = head2[u]; i != -1; i = edge2[i].next)
    {
		int v = edge2[i].v, id = edge2[i].id;
		if (!mark[v]) continue;
		anc[id] = findset(v);
	}
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].v;
		if (mark[v]) continue;
		LCA(v);
		father[v] = u;
	}
}

int search(int x)
{
	int i = 0;
	while (bin[i + 1] <= x) ++ i;
	return i;
}

int Minw(int u, int anc)
{
	int i = search(deep[u] - deep[anc] + 1);
	if (bin[i] == deep[u] - deep[anc] + 1) return minw[u][i];
	return min(minw[u][i], Minw(mp[u][i], anc));
}

int Maxw(int u, int anc)
{
	int i = search(deep[u] - deep[anc] + 1);
	if (bin[i] == deep[u] - deep[anc] + 1) return maxw[u][i];
	return max(maxw[u][i], Maxw(mp[u][i], anc));
}

int PushUp(int u, int anc)
{
	int i = search(deep[u] - deep[anc] + 1);
	if (bin[i] == deep[u] - deep[anc] + 1) return up[u][i];
	int upfa = PushUp(mp[u][i], anc);
	upfa = max(upfa, up[u][i]);
	int maxwfa = Maxw(mp[u][i], anc);
	return max(upfa, maxwfa - minw[u][i]);
}

int PushDown(int u, int anc)
{
	int i = search(deep[u] - deep[anc] + 1);
	if (bin[i] == deep[u] - deep[anc] + 1) return down[u][i];
	int downfa = PushDown(mp[u][i], anc);
	downfa = max(downfa, down[u][i]);
	int minwfa = Minw(mp[u][i], anc);
	return max(downfa, maxw[u][i] - minwfa);
}

int main(void)
{
	bin[0] = 1;
	for (int i = 1; bin[i - 1] < MAXN; ++i)bin[i] = bin[i - 1] * 2;
	int u, v;
	while (scanf("%d", &n) == 1)
    {
		Init(n);
		for (int i = 1; i <= n; ++ i) scanf("%d", ww + i);
		for (int i = 1; i < n; ++ i)
        {
			scanf("%d%d", &u, &v);
			Insert(u, v, i);
			Insert(v, u, i);
		}
		size = 0;
		scanf("%d", &m);
		for (int i = 0; i < m; ++ i)
		{
			scanf("%d%d", uu + i, vv + i);
			Insert2(uu[i], vv[i], i);
			Insert2(vv[i], uu[i], i);
		}
		dfs(1, -1, 1);
		RMQ(1, -1);
		LCA(1);
		for (int i = 0; i < m; ++ i)
		{
			int upmax = PushUp(uu[i], anc[i]), downmax = PushDown(vv[i], anc[i]);
			int Minww = Minw(uu[i], anc[i]), Maxww = Maxw(vv[i], anc[i]);
			printf("%d\n", max(max(upmax, downmax), Maxww - Minww));
		}
	}
	return 0;
}

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

题意:给出一棵节点有值的树,给出Q个询问(a,b),问从a到b的最大盈利(即:先在最小值买入,再在最大值卖出)

The merchant
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3344 Accepted: 1110

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



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值