码迷,mamicode.com
首页 > 其他好文 > 详细

[POJ 3728]The merchant

时间:2017-10-13 23:42:38      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:include   pst   ber   nes   最大   pac   using   pair   blog   

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

题解

题解:因为没有修改,所以我们可以使用树上倍增来解决,
设 $fa[i][j]$表示点 $i$ 的第 $2^j$ 个祖先
$ma[i][j]$表示点 $i$ 到点 $fa[i][j]$的最大值。
$mi[i][j]$表示点 $i$ 到点 $fa[i][j]$的最小值。
$zma[i][j]$表示点 $i$ 到点 $fa[i][j]$的最大获利。
$fma[i][j]$表示点 $fa[i][j]$到点 $i$ 的最大获利。
然后我们可以预处理出这四个数组。
即:

1 ma[x][i]=max(ma[fa[x][i-1]][i-1],ma[x][i-1]);
2 mi[x][i]=min(mi[fa[x][i-1]][i-1],mi[x][i-1]);
3 zma[x][i]=max(max(zma[fa[x][i-1]][i-1],zma[x][i-1]),ma[fa[x][i-1]][i-1]-mi[x][i-1]);
4 fma[x][i]=max(max(fma[fa[x][i-1]][i-1],fma[x][i-1]),ma[x][i-1]-mi[fa[x][i-1]][i-1]);

在走向最近公共祖先的路径上记录一下历史最小值,在远离最近公共祖先的路径上记录一下历史最大值(在途中和最大获利比较)。最后答案再和历史最大值-历史最小值比较一下即可。

 

  1 //It is made by Awson on 2017.10.13
  2 #include <set>
  3 #include <map>
  4 #include <cmath>
  5 #include <ctime>
  6 #include <stack>
  7 #include <queue>
  8 #include <string>
  9 #include <cstdio>
 10 #include <vector>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 #define LL long long
 16 #define Min(a, b) ((a) < (b) ? (a) : (b))
 17 #define Max(a, b) ((a) > (b) ? (a) : (b))
 18 using namespace std;
 19 const int N = 100000;
 20 const int INF = 1e9;
 21 
 22 int n, m, u, v, lim;
 23 struct tt {
 24   int to, next;
 25 }edge[(N<<1)+5];
 26 int path[N+5], top;
 27 int cost[N+5];
 28 int dep[N+5], fa[N+5][20], maxn[N+5][20], minn[N+5][20], up[N+5][20], down[N+5][20];
 29 
 30 void add(int u, int v) {
 31   edge[++top].to = v;
 32   edge[top].next = path[u];
 33   path[u] = top;
 34 }
 35 void dfs(int u, int depth) {
 36   dep[u] = depth;
 37   for (int i = path[u]; i; i = edge[i].next)
 38     if (dep[edge[i].to] == 0) {
 39       maxn[edge[i].to][0] = Max(cost[edge[i].to], cost[u]);
 40       minn[edge[i].to][0] = Min(cost[edge[i].to], cost[u]);
 41       up[edge[i].to][0] = Max(0, cost[u]-cost[edge[i].to]);
 42       down[edge[i].to][0] = Max(0, cost[edge[i].to]-cost[u]);
 43       fa[edge[i].to][0] = u;
 44       dfs(edge[i].to, depth+1);
 45     }
 46 }
 47 void ST() {
 48   for (int t = 1; t <= lim; t++)
 49     for (int i = 1; i <= n; i++) {
 50       int v = fa[i][t-1];
 51       fa[i][t] = fa[v][t-1];
 52       maxn[i][t] = Max(maxn[i][t-1], maxn[v][t-1]);
 53       minn[i][t] = Min(minn[i][t-1], minn[v][t-1]);
 54       up[i][t] = Max(up[i][t-1], up[v][t-1]), up[i][t] = Max(up[i][t], maxn[v][t-1]-minn[i][t-1]);
 55       down[i][t] = Max(down[i][t-1], down[v][t-1]), down[i][t] = Max(down[i][t], maxn[i][t-1]-minn[v][t-1]);
 56     }
 57 }
 58 int get_lca(int u, int v) {
 59   if (dep[u] < dep[v]) swap(u, v);
 60   for (int i = lim; i >= 0; i--)
 61     if (dep[fa[u][i]] >= dep[v])
 62       u = fa[u][i];
 63   if (u != v) {
 64     for (int i = lim; i >= 0; i--)
 65       if (fa[u][i] != fa[v][i])
 66     u = fa[u][i], v = fa[v][i];
 67     u = fa[u][0], v = fa[v][0];
 68   }
 69   return u;
 70 }
 71 int get_ans(int u, int v) {
 72   int lca = get_lca(u, v);
 73   int ans = 0, large = -INF, small = INF;
 74   for (int i = lim; i >= 0; i--)
 75     if (dep[fa[u][i]] >= dep[lca]) {
 76       ans = Max(ans, up[u][i]);
 77       ans = Max(ans, maxn[u][i]-small);
 78       small = Min(small, minn[u][i]);
 79       u = fa[u][i];
 80     }
 81   for (int i = lim; i >= 0; i--)
 82     if (dep[fa[v][i]] >= dep[lca]) {
 83       ans = Max(ans, down[v][i]);
 84       ans = Max(ans, large-minn[v][i]);
 85       large = Max(large, maxn[v][i]);
 86       v = fa[v][i];
 87     }
 88   return Max(ans, large-small);
 89 }
 90 
 91 void work() {
 92   scanf("%d", &n); lim = log(n*1.)/log(2*1.);
 93   for (int i = 1; i <= n; i++) scanf("%d", &cost[i]);
 94   for (int i = 1; i < n; i++) {
 95     scanf("%d%d", &u, &v);
 96     add(u, v), add(v, u);
 97   }
 98   dfs(1, 1); ST();
 99   scanf("%d", &m);
100   while (m--) {
101     scanf("%d%d", &u, &v);
102     printf("%d\n", get_ans(u, v));
103   }
104 }
105 int main() {
106   work();
107   return 0;
108 }

 

[POJ 3728]The merchant

标签:include   pst   ber   nes   最大   pac   using   pair   blog   

原文地址:http://www.cnblogs.com/NaVi-Awson/p/7663586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!