zoukankan      html  css  js  c++  java
  • 【BZOJ 3732】 Network

    【题目链接】

              点击打开链接

    【算法】

               求出这个图的最小生成树,对于每次询问,用倍增法求出最近公共祖先,查询最小生成树上两点路径上的最大值

               算法的正确性?

               假设x和y在最小生成树中路径上的最长边为p,那么,根据kruskal算法的执行过程,我们发现p合并

               了x和y所在的集合

               假设有一条边q,满足q < p且x和y路径上的最长边为q,根据kruskal算法的执行过程,我们发现这条边必然

               不能合并x和y所在的集合

               因此,不会有比p更短的边

    【代码】

              

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 15010
    #define MAXM 30010
    #define MAXLOG 20
    
    struct info
    {
            int x,y,d;
    } edge[MAXM];
    struct Edge
    {
            int to,w,nxt;
    } e[MAXN<<1];
    
    int i,tot,cnt,n,m,q,x,y,d;
    int fa[MAXN],dep[MAXN],anc[MAXN][MAXLOG],mx[MAXN][MAXLOG],head[MAXN];
    
    inline void add(int x,int y,int d)
    {
            tot++;
            e[tot] = (Edge){y,d,head[x]};
            head[x] = tot;
    }
    inline bool cmp(info a,info b) { return a.d < b.d; }
    inline int get_root(int x)
    {
            if (fa[x] == x) return x;
            return fa[x] = get_root(fa[x]);
    }
    inline void kruskal()
    {
            int i,sx,sy,x,y,d;
            for (i = 1; i <= n; i++) fa[i] = i;
            sort(edge+1,edge+m+1,cmp);
            for (i = 1; i <= m; i++)
            {
                    x = edge[i].x;
                    y = edge[i].y;
                    d = edge[i].d;
                    sx = get_root(x);
                    sy = get_root(y);
                    if (sx != sy)
                    {
                            fa[sx] = sy;
                            add(x,y,d);
                            add(y,x,d);
                    }
            }
    }
    inline void dfs(int u)
    {
            int i,v;
            for (i = 1; i < MAXLOG; i++) 
            {
                    if (dep[u] < (1 << i)) break;
                    anc[u][i] = anc[anc[u][i-1]][i-1];
                    mx[u][i] = max(mx[u][i-1],mx[anc[u][i-1]][i-1]);
            }
            for (i = head[u]; i; i = e[i].nxt)
            {
                    v = e[i].to;
                    if (anc[u][0] != v)
                    {
                            dep[v] = dep[u] + 1;
                            anc[v][0] = u;
                            mx[v][0] = e[i].w; 
                            dfs(v);
                    }
            }
    }
    inline int query(int x,int y)
    {
            int i,t,ans = 0;
            if (dep[x] > dep[y]) swap(x,y);
            t = dep[y] - dep[x];
            for (i = 0; i < MAXLOG; i++)
            {
                    if (t & (1 << i))
                    {
                            ans = max(ans,mx[y][i]);
                            y = anc[y][i];
                    }
            }
            if (x == y) return ans;
            for (i = MAXLOG - 1; i >= 0; i--)
            {
                    if (anc[x][i] != anc[y][i])
                    {
                            ans = max(ans,max(mx[x][i],mx[y][i]));
                            x = anc[x][i];
                            y = anc[y][i];
                    }
            }
            return max(ans,max(mx[x][0],mx[y][0]));
    }
    int main() 
    {
    
            scanf("%d%d%d",&n,&m,&q);
            for (i = 1; i <= m; i++)
            {
                    scanf("%d%d%d",&x,&y,&d);
                    edge[++cnt] = (info){x,y,d};    
            }        
            kruskal();
            dfs(1);
            while (q--)
            {
                    scanf("%d%d",&x,&y);
                    printf("%d
    ",query(x,y));
            }
            
            return 0;
        
    }

               

  • 相关阅读:
    基于摸板匹配的目標跟蹤算法
    spoj 2713 Can you answer these queries IV
    zoj 3633 Alice's present
    hdu 3642 Get The Treasury
    poj 1195 Mobile phones
    poj 2760 End of Windless Days
    zoj 3540 Adding New Machine
    spoj 1716 Can you answer these queries III
    spoj 1043 Can you answer these queries I
    spoj 2916 Can you answer these queries V
  • 原文地址:https://www.cnblogs.com/evenbao/p/9196294.html
Copyright © 2011-2022 走看看