http://poj.org/problem?id=3728
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5068 | Accepted: 1744 |
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 ≤ 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
Source
1 #include <cstdio> 2 3 using namespace std; 4 5 const int INF(999999999); 6 const int N(200000+5); 7 int pri[N],sumedge,head[N]; 8 struct Edge 9 { 10 int v,next; 11 Edge(int v=0,int next=0):v(v),next(next){} 12 }edge[N<<1]; 13 inline void ins(int u,int v) 14 { 15 edge[++sumedge]=Edge(v,head[u]); 16 head[u]=sumedge; 17 } 18 19 #define swap(a,b) {int tmp=a;a=b;b=tmp;} 20 #define max(a,b) (a>b?a:b) 21 #define min(a,b) (a<b?a:b) 22 struct Type_Node 23 { 24 int up_down,down_up; 25 int maxx,minn,dad; 26 }shop[N][23]; 27 int deep[N]; 28 void DFS(int x,int fa) 29 { 30 deep[x]=deep[fa]+1; 31 shop[x][0].maxx=max(pri[x],pri[fa]); 32 shop[x][0].minn=min(pri[x],pri[fa]); 33 shop[x][0].down_up=max(0,pri[fa]-pri[x]); 34 shop[x][0].up_down=max(0,pri[x]-pri[fa]); 35 for(int i=1;shop[x][i-1].dad;i++) 36 { 37 shop[x][i].dad=shop[shop[x][i-1].dad][i-1].dad; 38 shop[x][i].maxx=max(shop[x][i-1].maxx,shop[shop[x][i-1].dad][i-1].maxx); 39 shop[x][i].minn=min(shop[x][i-1].minn,shop[shop[x][i-1].dad][i-1].minn); 40 shop[x][i].down_up=max(shop[x][i-1].down_up,shop[shop[x][i-1].dad][i-1].down_up); 41 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); 42 shop[x][i].up_down=max(shop[x][i-1].up_down,shop[shop[x][i-1].dad][i-1].up_down); 43 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); 44 } 45 for(int i=head[x];i;i=edge[i].next) 46 { 47 int v=edge[i].v; 48 if(shop[x][0].dad!=v) shop[v][0].dad=x,DFS(v,x); 49 } 50 } 51 int LCA(int x,int y) 52 { 53 if(deep[x]>deep[y]) swap(x,y); 54 for(int i=20;i>=0;i--) 55 if(deep[shop[y][i].dad]>=deep[x]) y=shop[y][i].dad; 56 if(x==y) return x; 57 for(int i=20;i>=0;i--) 58 if(shop[x][i].dad!=shop[y][i].dad) x=shop[x][i].dad,y=shop[y][i].dad; 59 return shop[x][0].dad; 60 } 61 int Query(int x,int y) 62 { 63 int ans=0,maxx=-INF,minn=INF,lca=LCA(x,y); 64 for(int i=20;i>=0;i--) 65 if(deep[shop[x][i].dad]>=deep[lca]) 66 { 67 ans=max(ans,max(shop[x][i].down_up,shop[x][i].maxx-minn)); 68 minn=min(minn,shop[x][i].minn); 69 x=shop[x][i].dad; 70 } 71 for(int i=20;i>=0;i--) 72 if(deep[shop[y][i].dad]>=deep[lca]) 73 { 74 ans=max(ans,max(shop[y][i].up_down,maxx-shop[y][i].minn)); 75 maxx=max(maxx,shop[y][i].maxx); 76 y=shop[y][i].dad; 77 } 78 return max(ans,maxx-minn); 79 } 80 81 inline void read(int &x) 82 { 83 x=0;register char ch=getchar(); 84 for(;ch<'0'||ch>'9';) ch=getchar(); 85 for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; 86 } 87 88 int main() 89 { 90 int n; read(n); 91 for(int i=1;i<=n;i++) read(pri[i]); 92 for(int u,v,i=1;i<n;i++) 93 { 94 read(u),read(v); 95 ins(u,v),ins(v,u); 96 } 97 DFS(1,0); 98 int q; read(q); 99 for(int u,v;q--;) 100 { 101 read(u),read(v); 102 if(u==v) puts("0"); 103 else printf("%d ",Query(u,v)); 104 } 105 return 0; 106 }