您的位置:首页 > 其它

poj 3728 LCA+并查集 解题报告

2017-08-22 17:17 417 查看

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

思路

离线LCA。

但是网上几乎是DP,不过我觉得并查集也可以啊。。。

具体看代码的注释吧。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=50010;
vector<int> g
;
vector<pair<int,int> > qu
;
vector<pair<int,int> > ans
;//保存该节点作为lca,它的子节点中都有哪些查询
int res
;
int n,q,val
,pre
,vis
;
//up表示从当前节点到当前lca的最大盈利,down表示从从当前lca到当前节点的最大盈利,maxv表示节点到lca的最大值,minv表示最小值
//这样查询u->v,就是比较up[u],down[v]和maxv[v]-minv[u]的最大值
int up
,down
,maxv
,minv
;
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;
}


彩蛋

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: