UVA 11354 Bond(MST + LCA)

n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路。

其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法。但是对于这个题的n,邻接矩阵是存不下的。。。所以默默的抄了一遍大白书上的算法。。。先用kruskal求MST,然后对于MST树,每次询问求p和q的LCA,在求LCA的过程中顺便求出瓶颈路。。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
using namespace std;

const int maxn = 50005;
const int maxm = 100010;
const int INF = 1e9;
int n, m;
int fa[maxn], pa[maxn], cost[maxn], L[maxn], anc[maxn][100], maxcost[maxn][100];
struct Edge
{
    int to, dist;
};
vector<Edge> edges;
vector<int> G[maxn];
inline void add(int a, int b, int c)
{
    edges.PB((Edge){b, c});
    edges.PB((Edge){a, c});
    int nc = edges.size();
    G[a].PB(nc-2); G[b].PB(nc-1);
}

inline void init()
{
    L[1] = cost[1] = 0;
    REP(i, n+1) pa[i] = i;
    REP(i, n+1) G[i].clear(); edges.clear();
}

int findset(int x) {    return x == pa[x] ? x : pa[x] = findset(pa[x]); }

struct E
{
    int u, v, w;
    bool operator < (const E rhs) const
    {
        return w < rhs.w;
    }
}e[maxm];

void MST()
{
    sort(e, e+m);
    int cnt = 0;
    REP(i, m)
    {
        int x = findset(e[i].u), y = findset(e[i].v);
        if(x != y)
        {
            pa[x] = y;
            cnt++;
            add(e[i].u, e[i].v, e[i].w);
        }
        if(cnt == n-1) return ;
    }
}

void dfs(int u, int f)
{
    fa[u] = f;
    int nc = G[u].size();
    REP(i, nc)
    {
        int v = edges[G[u][i]].to, w = edges[G[u][i]].dist;
        if(v != f)
        {
            cost[v] = w;
            L[v] = L[u] + 1;
            dfs(v, u);
        }
    }
}

void progress()
{
    FF(i, 1, n+1)
    {
        anc[i][0] = fa[i], maxcost[i][0] = cost[i];
        for(int j=1; (1 << j) < n; j++) anc[i][j] = -1;
    }
    for(int j=1; (1 << j) < n; j++) FF(i, 1, n+1)
    if(anc[i][j-1] != -1)
    {
        int a = anc[i][j-1];
        anc[i][j] = anc[a][j-1];
        maxcost[i][j] = max(maxcost[i][j-1], maxcost[a][j-1]);
    }
}

int query(int p, int q)
{
    int lo;
    if(L[p] < L[q]) swap(p, q);
    for(lo = 1; (1 << lo) <= L[p]; lo++); lo--;

    int ans = -INF;
    FD(i, lo, 0)
        if(L[p] - (1<<i) >= L[q]) ans = max(ans, maxcost[p][i]), p = anc[p][i];

    if(p == q) return ans; //LCA -> p

    FD(i, lo, 0)
    if(anc[p][i] != -1 && anc[p][i] != anc[q][i])
    {
        ans = max(ans, maxcost[p][i]), p = anc[p][i];
        ans = max(ans, maxcost[q][i]), q = anc[q][i];
    }

    ans = max(ans, cost[p]);
    ans = max(ans, cost[q]);
    return ans; //LCA -> fa[q] fa[p]
}

int main()
{
    int flag = 0;
    while(~scanf("%d%d", &n, &m))
    {
        if(flag++) puts("");

        init();
        REP(i, m) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);

        MST();
        dfs(1, -1);
        progress();

        int Q, p, q;
        scanf("%d", &Q);
        while(Q--)
        {
            scanf("%d%d", &p, &q);
            printf("%d\n", query(p, q));
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值