UVA 11354 Bond(MST+LCA/二进制优化)

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2339

题目大意:有n个城市,m条双向道路,q个询问,每个询问包含两个点s、t,问你找出一条s到t的路,使得路径上所有边的最大危险系数最小。输出这个最大值。

解题思路:这道题也就是求最大瓶颈路,但是要快速回答每个询问,而点数又多,不能像之前那样,把所有的都算出来。但是这道题也可以考虑先做预处理,把信息组织成某种易于查询的结构。首先求出最小生成树,并把它改成有根树。cost[ i ] 和fa [ i ] 表示 i 节点的父亲节点编号和 i 与 它父亲节点的边的权值。L[ i ] 记录节点的深度,根的深度为 0 。cost、fa、L可由一遍遍历得出。然后计算anc 和 max_cost 数组,anc[ i ][ j ]表示节点 i 的往上第 2^j 级祖先的编号,max_cost[ i ][ j ] 则表示i 和 它的第 2^j 级祖先唯一路径上的最大边权。对于这两个,有转移方程:anc[ i ][ j ] = anc[ anc[ i ][ j - 1] ][ j - 1] ,max_cost[ i ][ j ] = max(max_cost[ i ][ j - 1] , max_cost[ anc[ i ][ j - 1] ][ j - 1 ]),同时可由两个for循环求出,初始值anc[ i ][ 0 ] = fa[ i ],max_cost[ i ][ 0 ] = cost[ i ]。

        以上是预处理部分,接下来就是查询了。假设查询的是 p、q。那么假设 l[ p ] > l[ q ] (如果不是,swap一下就好),把 p 先往上提,直到和 q 的深度相同。然后在将 p、q 一起往上提,到达最近公共祖先。不难得出,路径上所有的 max_cost 的 max 就是答案了。LCA的过程用二进制来优化。首先是将 p 提到和 q 同个高度的过程:将 l[ p ] 中 l[ q ] 二进制中没有的 1 全部剃掉。然后是一起同步提高的过程:将二者二进制位数上的 1 全部剃掉。具体过程见代码。其实提到最后的 p、q 是他们LCA 的子节点,也就是说他们的LCA 是 fa[ p ] (=fa[ q ])。

         好吧,这道题TLE了n次,就是不知道那里死循环了,最后发现竟然是query 那里的代码打残了,敲错了两个地方,看着书敲都能敲残,my god! T T

代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

const int INF = 0x0fffffff;

const int MAXN = 51111;
const int MAXM = 111111;

struct Edge
{
    int s,t,val;
    void read()
    {
        scanf("%d%d%d",&s,&t,&val);
        s--,t--;
    }
    bool operator < (const Edge& tmp)const
    {
        return val < tmp.val;
    }
} edge[MAXM];

int fa[MAXN];

int find_fa(int x)
{
    if(x == fa[x]) return x;
    return fa[x] = find_fa(fa[x]);
}

vector< pair<int,int> > G[MAXN];

void krus(int n,int m)
{
    for(int i = 0;i < n;i++) fa[i] = i,G[i].clear();
    sort(edge,edge+m);
    int num = 0;
    for(int i = 0;i < m;i++)
    {
        int a = edge[i].s;
        int b = edge[i].t;
        int fx = find_fa(a);
        int fy = find_fa(b);
        if(fx == fy) continue;
        fa[fx] = fy;
        G[a].push_back(make_pair(b,edge[i].val));
        G[b].push_back(make_pair(a,edge[i].val));
        num++;
        if(num == n-1) break;
    }
}

int f[MAXN],l[MAXN],cost[MAXN];

void dfs(int u,int father)
{
    f[u] = father;
    for(int i = 0;i < G[u].size();i++)
    {
        int v = G[u][i].first;
        if(v != father)
        {
            l[v] = l[u]+1;
            cost[v] = G[u][i].second;
            dfs(v,u);
        }
    }
}

int anc[MAXN][22],max_cost[MAXN][22];

void preprocess(int n)
{
    for(int i = 0;i < n;i++)
    {
        anc[i][0] = f[i];
        max_cost[i][0] = cost[i];
        for(int j = 1;(1<<j) < n;j++)
            anc[i][j] = -1;
    }
    for(int j = 1;(1<<j) < n;j++)
        for(int i = 0;i < n;i++)
            if(anc[i][j-1] != -1)
            {
                int a = anc[i][j-1];
                anc[i][j] = anc[a][j-1];
                max_cost[i][j] = max(max_cost[i][j-1],max_cost[a][j-1]);
            }
}

int query(int p,int q)
{
    if(l[p] < l[q]) swap(p,q);
    int log;
    for(log = 1;(1<<log) <= l[p];log++);
    log--;
    int ans = -INF;
    for(int i = log;i >= 0;i--)
        if(l[p]-(1<<i) >= l[q])
        {
            ans = max(ans,max_cost[p][i]);
            p = anc[p][i];
        }
    if(p == q) return ans;
    for(int i = log;i >= 0;i--)
    {
        if(anc[p][i] != -1 && anc[p][i] != anc[q][i])
        {
            ans = max(ans,max_cost[p][i]); p = anc[p][i];
            ans = max(ans,max_cost[q][i]); q = anc[q][i];
        }
    }
    ans = max(ans,cost[p]);
    ans = max(ans,cost[q]);
    return ans;
}

int main()
{
    int cas = 0;
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        if(cas++) puts("");
        int tot = 0;
        while(m--)
        {
            edge[tot++].read();
        }
        krus(n,tot);
        l[0] = 0;
        dfs(0,-1);
        preprocess(n);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            a--,b--;
            printf("%d\n",query(a,b));
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值