zoukankan      html  css  js  c++  java
  • POJ1986(LCA应用:求两结点之间距离)

    Distance Queries
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 11304   Accepted: 3985
    Case Time Limit: 1000MS

    Description

    Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

    Input

    * Lines 1..1+M: Same format as "Navigation Nightmare" 

    * Line 2+M: A single integer, K. 1 <= K <= 10,000 

    * Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

    Output

    * Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

    Sample Input

    7 6
    1 6 13 E
    6 3 9 E
    3 5 7 S
    4 1 3 N
    2 4 20 W
    4 7 2 S
    3
    1 6
    1 4
    2 6
    

    Sample Output

    13
    3
    36
    学习LCA的好文章:http://taop.marchtea.com/04.04.html
    离线:将所有查询输入完毕后再统一输出结果。
    在线:查询一个输出一个。

    dfs+并查集,离线
    #include <cstdio>
    #include <vector>
    using namespace std;
    const int MAXN=50005;
    int n,m,k;
    struct Edge{
        int to,w;
        Edge(){}
        Edge(int to,int w)
        {
            this->to=to;
            this->w=w;
        }
    };
    vector<Edge> arc[MAXN];
    
    struct Node{
        int to,id;
        Node(){}
        Node(int to,int id)
        {
            this->to=to;
            this->id=id;
        }
    };
    vector<Node> que[MAXN];
    
    int par[MAXN];
    void prep()
    {
        for(int i=0;i<MAXN;i++)
        {
            d[i]=0;
            vis[i]=0; 
            par[i]=i;
        }
    }
    int fnd(int x)
    {
        if(par[x]==x)
        {
            return x;
        }
        return par[x]=fnd(par[x]);
    }
    void unite(int fa,int son)
    {
        int a=fnd(fa);
        int b=fnd(son);
        par[b]=a;
    }
    
    int vis[MAXN],d[MAXN];
    int res[MAXN];
    void tarjan(int u)
    {
        vis[u]=1;
        for(int i=0,size=que[u].size();i<size;i++)
        {
            Node nod=que[u][i];
            if(vis[nod.to])
            {
                int lca=fnd(nod.to);
                res[nod.id]=d[nod.to]+d[u]-2*d[lca];
            }
        }
        for(int i=0,size=arc[u].size();i<size;i++)
        {
            Edge e=arc[u][i];
            if(!vis[e.to])
            {
                d[e.to]=d[u]+e.w;
                tarjan(e.to);
                unite(u,e.to);
            }
        }
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            prep();
            for(int i=1;i<=n;i++)    arc[i].clear();
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf("%d %d %d %*c",&u,&v,&w);
                arc[u].push_back(Edge(v,w));
                arc[v].push_back(Edge(u,w));
            }
            scanf("%d",&k);
            for(int i=0;i<k;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                que[v].push_back(Node(u,i));
                que[u].push_back(Node(v,i));
            }
            for(int i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    tarjan(i);
                }
            }
            for(int i=0;i<k;i++)
            {
                printf("%d
    ",res[i]);
            }
        }
        return 0;
    }
    模板:RMQ求LCA在线算法(稀疏表实现RMQ)
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    const int MAXN=50005;
    int n,m,k;
    struct Edge{
        int to,w;
        Edge(){}
        Edge(int to,int w)
        {
            this->to=to;
            this->w=w;
        }
    };
    vector<Edge> arc[MAXN];
    
    int vs[MAXN+MAXN],depth[MAXN+MAXN],first[MAXN],tot;
    int d[MAXN],vis[MAXN];
    void dfs(int u,int dep)
    {
        vis[u]=1;
        vs[++tot]=u;
        depth[tot]=dep;
        first[u]=tot;
        for(int i=0,size=arc[u].size();i<size;i++)
        {
            Edge e=arc[u][i];
            if(!vis[e.to])
            {
                d[e.to]=d[u]+e.w;
                dfs(e.to,dep+1);
                vs[++tot]=u;
                depth[tot]=dep;
            }
        }
    }
    
    int dp[MAXN+MAXN][21];
    void init_st(int size)
    {
        for(int i=1;i<=size;i++)    dp[i][0]=i;
        for(int j=1;j<21;j++)
        {
            for(int i=1;i<=size;i++)
            {
                if(i+(1<<j)-1<=size)
                {
                    int a=dp[i][j-1];
                    int b=dp[i+(1<<(j-1))][j-1];
                    dp[i][j]=depth[a]<depth[b]?a:b;
                }    
            }
        }
    }
    int rmq_st(int l,int r)
    {    
        int limit=(int)(log(r-l+1.0)/(log(2.0)));
        int a=dp[l][limit];
        int b=dp[r-(1<<limit)+1][limit];
        return depth[a]<depth[b]?a:b;
    }
    
    int LCA(int u,int v)
    {
        if(first[u]>first[v])    swap(u,v);
        int id=rmq_st(first[u],first[v]);
        return vs[id];
    }
    
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            tot=0;
            memset(vis,0,sizeof(vis));
            memset(d,0,sizeof(d));
            for(int i=1;i<=n;i++)    arc[i].clear();
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf("%d %d %d %*c",&u,&v,&w);
                arc[u].push_back(Edge(v,w));
                arc[v].push_back(Edge(u,w));
            }
            for(int i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    dfs(i,1);
                }
            }
            init_st(tot);
            scanf("%d",&k);
            for(int i=0;i<k;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                int lca=LCA(u,v);
                int res=d[u]+d[v]-2*d[lca];
                printf("%d
    ",res);
            }
        }
        return 0;
    }


  • 相关阅读:
    ajax 上传文件
    在linux服务器centos上使用svn同步代码到项目中
    css3 选择器 权重问题 (第二部分)
    css3 选择器 权重问题 (第一部分)
    css3 文本模型
    (java)剑指offer题三
    (java)剑指0ffer题二
    (java)剑指offer题一
    java程序入口main()方法浅析
    jar命令浅析
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5172370.html
Copyright © 2011-2022 走看看