LCA+并查集应用(好题)poj3728
来源:互联网 发布:图标设计图案软件 编辑:程序博客网 时间:2024/04/30 00:56
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
41 5 3 21 33 23 491 21 31 42 32 12 43 13 23 4
Sample Output
422000020题意:给出一棵节点有值的树,给出Q个询问(a,b),问从a到b的最大盈利(即:先在最小值买入,再在最大值卖出)
思路:LCA离线算法,维护四个值,up,down,minv,maxv,详见代码
#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=50010;vector<int> g[maxn];vector<pair<int,int> > qu[maxn];vector<pair<int,int> > ans[maxn];//保存该节点作为lca,它的子节点中都有哪些查询int res[maxn];int n,q,val[maxn],pre[maxn],vis[maxn];//up表示从当前节点到当前lca的最大盈利,down表示从从当前lca到当前节点的最大盈利,maxv表示节点到lca的最大值,minv表示最小值//这样查询u->v,就是比较up[u],down[v]和maxv[v]-minv[u]的最大值int up[maxn],down[maxn],maxv[maxn],minv[maxn];int find(int x){ if(x==pre[x])return x;; int fa=pre[x]; pre[x]=find(pre[x]); up[x]=max(up[x],max(up[fa],maxv[fa]-minv[x]));//刚开始想求最大值和最小值得点的顺序不一定符合要求,为什么要这样更新,后来发现minv和maxv初始值就是本节点的值 down[x]=max(down[x],max(down[fa],maxv[x]-minv[fa])); maxv[x]=max(maxv[x],maxv[fa]); minv[x]=min(minv[x],minv[fa]); return pre[x];}void LCA(int u){ int len=qu[u].size(); for(int i=0;i<len;i++) { int v=qu[u][i].second; if(vis[v]) { int lca=find(v); ans[lca].push_back(make_pair(u,i));//把询问添加到lca,这样当该LCA节点的所有子节点访问完之后,就可以一起更新答案了 } } vis[u]=1; pre[u]=u; len=g[u].size(); for(int i=0;i<len;i++) { int v=g[u][i]; if(vis[v])continue; LCA(v); pre[v]=u; } len=ans[u].size(); for(int i=0;i<len;i++) { int x=ans[u][i].first,y=qu[x][ans[u][i].second].second; int id=qu[x][ans[u][i].second].first; if(id<0){id=-id;swap(x,y);}//保证顺序是对的 find(x);find(y); res[id]=max(up[y],down[x]); res[id]=max(res[id],maxv[x]-minv[y]); }}int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&val[i]); memset(up,0,sizeof(up)); memset(down,0,sizeof(down)); for(int i=1;i<=n;i++) maxv[i]=minv[i]=val[i]; for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } scanf("%d",&q); for(int i=1;i<=q;i++) { int u,v; scanf("%d%d",&u,&v); qu[u].push_back(make_pair(-i,v)); qu[v].push_back(make_pair(i,u)); } memset(vis,0,sizeof(vis)); LCA(1); for(int i=1;i<=q;i++)printf("%d\n",res[i]); return 0;}
- LCA+并查集应用(好题)poj3728
- poj3728并查集+LCA
- poj3728 The merchant LCA+RMQ或 LCA+b并查集 好题!
- poj3728 The merchant LCA+并查集
- poj3728 The merchant LCA(带权并查集?)
- LCA算法--并查集应用
- poj 3728(LCA+并查集应用)
- HDU2874并查集+(LCA-RMQ)
- HDU6074(LCA+并查集)
- bzoj 3910(LCA+并查集)
- 10158 - War---挑战编程-----并查集应用好题!!!
- 并查集扩展应用(好)uva12232
- noip2013day1第三题 货车运输LCA+并查集
- 【POJ1330】最近公共祖先(LCA):并查集+深搜
- poj 3728(LCA+并查集+DP)
- 【LCA】Tarjan离线算法(并查集+dfs)模板
- 【POJ1330】最近公共祖先(LCA):并查集+深搜
- 最近公共祖先(LCA):并查集+深搜
- 前端选择哪种JavaScript framework
- JavaScript stopPropagation() 方法
- 研究完zd394071264《移植SYSZUXpinyin到tiny6410》后自己的修改与优化
- Candy Java
- GitHub上最火的40个Android开源项目(一)
- LCA+并查集应用(好题)poj3728
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- ftp工具类
- GitHub上最火的40个Android开源项目(二)
- 解决ubuntu14.04下Qt 5.3.1 下的QtCreator fcitx,ibus不能输入中文
- Android目录结构
- windows mysql提示:1045 access denied for user 'root'@'localhost' using password ye
- 通过用户id输出头像文件流
- Codeforces Round #248 (Div. 2) —— B