poj 3728 LCA+并查集 解题报告

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

彩蛋

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值