poj 3728 The merchant// lca(倍增实现) + dp
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6437 Accepted: 2251 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
题意:给出一个树,结点有一个w[i],给出q个询问,每个询问给出u和v,在u->v的路径中选出两个点a,b(a,b可以一样,a比b先出现),求出最大的w[b]-w[a]。
对于一个询问u,v,ans=max(u到lca(u,v)之间的最大值,lca(u,v)到v之间的最大值,根节点到v之间的最大值-根节点到u之间的最小值)。
利用倍增思想,处理出fa[v][k](v号结点上升2^k所到的结点),mx[v][k],mi[v][k](v号结点上升2^k所到结点x,x,v之间的最大值及最小值),up[v][k](v号结点上升2^k所到结点x,v和x之间能赚到钱的最大值)down[v][k](从距离v 2^k步所在的结点x下降到v,x和v之间能赚到钱的最大值)。
具体dp转移的过程看注释。这题还是挺有质量的。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
const int max_n=5e4+5,max_logn=24;
struct no{int to,ne;};
no e[max_n*2];int head[max_n],edgenum;
void initvector(){edgenum=0;memset(head,-1,sizeof(head));}
void addedge(int from,int to){//头插法
no E={to,head[from]};
e[edgenum]=E;head[from]=edgenum++;
}
int fa[max_n][max_logn],de[max_n],root=1;
int n,w[max_n];
int mx[max_n][max_logn],mi[max_n][max_logn];
int up[max_n][max_logn],down[max_n][max_logn];
void dfs(int v,int p,int d){//所有都是向上走2^k步 所代表的含义
fa[v][0]=p;de[v]=d;
mx[v][0]=max(w[v],w[p]);mi[v][0]=min(w[v],w[p]);//只走2^0次步 预处理
up[v][0]=max(w[p]-w[v],0);
down[v][0]=max(w[v]-w[p],0);
for(int i=head[v];i!=-1;i=e[i].ne){
if(e[i].to!=p)dfs(e[i].to,v,d+1);
}
}
void init(int V){//预处理fa,dp的数组
dfs(root,-1,0);
for(int k=0;k+1<max_logn;k++){
for(int v=1;v<=V;v++){
int tfa=fa[v][k];
if(tfa<0) fa[v][k+1]=-1;
else {//可达
fa[v][k+1]=fa[tfa][k];
mx[v][k+1]=max(mx[v][k],mx[tfa][k]);
mi[v][k+1]=min(mi[v][k],mi[tfa][k]);
up[v][k+1]=max(up[v][k],up[tfa][k]);
up[v][k+1]=max(up[v][k+1],mx[tfa][k]-mi[v][k]);//跨两个小区间
down[v][k+1]=max(down[v][k],down[tfa][k]);
down[v][k+1]=max(down[v][k+1],mx[v][k]-mi[tfa][k]);//和up相反
}
}
}
}
int find_lca(int u,int v){
if(de[u]>de[v]) swap(u,v);
for(int k=0;k<max_logn;k++){
if((de[v]-de[u])>>k&1) v=fa[v][k];
}if(u==v) return u;
for(int k=max_logn-1;k>=0;k--){
if(fa[u][k]!=fa[v][k]){u=fa[u][k],v=fa[v][k];}
}
return fa[u][0];
}
int find_up(int u,int lca,int &remin){
remin=INT_MAX/2;
int maxup=0,pre_min=INT_MAX/2;//维护前缀最小值
for(int k=max_logn-1;k>=0;k--){
if((de[u]-de[lca])>>k&1){
remin=min(remin,mi[u][k]);
maxup=max(maxup,up[u][k]);
maxup=max(maxup,mx[u][k]-pre_min);//跨小区间跟新
pre_min=min(pre_min,mi[u][k]);
u=fa[u][k];
}
}
return maxup;
}
int find_down(int v,int lca,int &remax){
remax=INT_MIN/2;
int maxdown=0,pre_max=INT_MIN/2;
for(int k=max_logn-1;k>=0;k--){
if((de[v]-de[lca])>>k&1){
remax=max(remax,mx[v][k]);
maxdown=max(maxdown,down[v][k]);
maxdown=max(maxdown,pre_max-mi[v][k]);
pre_max=max(pre_max,mx[v][k]);
v=fa[v][k];
}
}
return maxdown;
}
int find_ans(int u,int v){
int lca=find_lca(u,v);int re=0;
int up_min_val=INT_MAX/2,down_max_val=INT_MIN/2;
re=max(re,find_up(u,lca,up_min_val));//u-lca
re=max(re,find_down(v,lca,down_max_val));//lca-v
re=max(re,(down_max_val-up_min_val));//包含根节点
return re;
}
int main(){
initvector();
scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&w[i]);
for(int i=1;i<n;i++){
int u,v;scanf("%d%d",&u,&v);
addedge(u,v),addedge(v,u);
}
init(n);
int q;scanf("%d",&q);while(q--){
int xx,yy;scanf("%d%d",&xx,&yy);
printf("%d\n",find_ans(xx,yy));
}
return 0;
}
4.24更:
终于肝出来了并查集路径下dp+离线tarjanlca的解法
照样维护up,down,mi,mx,不过都是当前状态下的。
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int max_n= 5e4+5;
struct qno{int to,id;};
vector<qno>q[max_n];//询问
int ans[max_n];
vector<int>g[max_n];//树
int w[max_n],uu[max_n],vv[max_n];int n,Q;//uu:询问的起点,vv:询问的终点
int fa[max_n],vis[max_n];
int up[max_n],down[max_n],mx[max_n],mi[max_n];//都是在当前状态下,到父节点的关系
struct lcano{int x,id;};//lca下的询问
vector<lcano>qq[max_n];
int found(int x){//并查集路径下dp
if(fa[x]==x)return x;
int t=fa[x];
fa[x]=found(fa[x]);
up[x]=max(up[t],up[x]);
up[x]=max(up[x],mx[t]-mi[x]);
down[x]=max(down[t],down[x]);
down[x]=max(down[x],mx[x]-mi[t]);
mx[x]=max(mx[t],mx[x]);
mi[x]=min(mi[t],mi[x]);
return fa[x];
}
void tarjan(int u){
vis[u]=true;
for(int i=0;i<q[u].size();i++){
int qid=q[u][i].id,to=q[u][i].to; //lca为found(to)
if(vis[to]){
int lca=found(to);
qq[lca].push_back(lcano{to,qid});//先存起来,只有lca以下的节点都访问过了才可以计算
}
}
for(int i=0;i<g[u].size();i++){
int to=g[u][i];
if(!vis[to]){
tarjan(to);
fa[to]=u;
}
}
for(int i=0;i<qq[u].size();i++){//u子树都访问过了,那么u做为询问的lca,可以执行lca是u的询问,回溯的遍历顺序保证正确可行性
int id=qq[u][i].id;
found(uu[id]),found(vv[id]);//左边更新,右边跟新
ans[id]=max(up[uu[id]],down[vv[id]]);
ans[id]=max(ans[id],mx[vv[id]]-mi[uu[id]]);
}
}
int main(){
scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&w[i]);
for(int u=1;u<=n;u++){
fa[u]=u;up[u]=0;down[u]=0;mx[u]=w[u];mi[u]=w[u];
}
for(int i=1;i<n;i++){
int scu,scv;scanf("%d%d",&scu,&scv);
g[scu].push_back(scv);g[scv].push_back(scu);
}
scanf("%d",&Q);for(int i=1;i<=Q;i++){
scanf("%d%d",&uu[i],&vv[i]);
q[uu[i]].push_back(qno{vv[i],i});
q[vv[i]].push_back(qno{uu[i],i});
}
tarjan(1);
for(int i=1;i<=Q;i++)printf("%d\n",ans[i]);
return 0;
}
/*6
5 3 1 4 2 7
1 2
1 3
2 4
2 5
3 6
2
2 4
4 2*/