LCA+并查集应用(好题)poj3728

Language:
The merchant
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3143 Accepted: 1047

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 ≤ NwiQ ≤ 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
题意:给出一棵节点有值的树,给出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;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LCA+路径压缩的方式可以用于求解树上的桥,具体实现步骤如下: 1. 对于树上每个节点,记录其在树中的深度(或者高度)以及其父亲节点。 2. 对于每个节点,记录其在树上的最小深度(或最小高度)以及其所在子树中深度最小的节点。 3. 对于每条边(u, v),设u的深度小于v的深度(或者高度),则如果v的子树中没有深度小于u的节点,则(u, v)是桥。 具体的实现过程如下: 首先,我们需要对树进行预处理,求出每个节点的深度以及其父亲节点。可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来实现。在这里我们使用DFS来实现: ```c++ vector<int> adj[MAX_N]; // 树的邻接表 int n; // 树的节点数 int dep[MAX_N], fa[MAX_N]; // dep[i]表示节点i的深度,fa[i]表示节点i的父亲节点 void dfs(int u, int f, int d) { dep[u] = d; fa[u] = f; for (int v : adj[u]) { if (v != f) { dfs(v, u, d + 1); } } } ``` 接下来,我们需要计算每个节点所在子树中深度最小的节点。我们可以使用LCA(最近公共祖先)的方法来实现。具体来说,我们可以使用倍增算法来预处理出每个节点的2^k级祖先,并且在查询LCA时使用路径压缩的方式优化时间复杂度。这里我们不展开讲解LCA和倍增算法的细节,如果你对此感兴趣,可以参考其他资料进行学习。 ```c++ const int MAX_LOG_N = 20; // log2(n)的上取整 int anc[MAX_N][MAX_LOG_N]; // anc[i][j]表示节点i的2^j级祖先 int mn[MAX_N]; // mn[i]表示节点i所在子树中深度最小的节点 void precompute() { // 预处理anc数组 for (int j = 1; j < MAX_LOG_N; j++) { for (int i = 1; i <= n; i++) { if (anc[i][j - 1] != -1) { anc[i][j] = anc[anc[i][j - 1]][j - 1]; } } } // 计算mn数组 for (int i = 1; i <= n; i++) { mn[i] = i; for (int j = 0; (1 << j) <= dep[i]; j++) { if ((dep[i] & (1 << j)) != 0) { mn[i] = min(mn[i], mn[anc[i][j]]); i = anc[i][j]; } } } } ``` 最后,我们可以使用LCA+路径压缩的方式来判断每条边是否为桥。具体来说,对于每条边(u, v),我们需要判断v的子树中是否存在深度小于u的节点。如果存在,则(u, v)不是桥,否则(u, v)是桥。 ```c++ bool is_bridge(int u, int v) { if (dep[u] > dep[v]) swap(u, v); if (mn[v] != u) return true; // 子树中存在深度小于u的节点 return false; // 子树中不存在深度小于u的节点 } ``` 完整代码如下:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值