zoukankan      html  css  js  c++  java
  • poj1986 LCA

    Distance Queries
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 11759   Accepted: 4157
    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

    题意:

    n个点,m条边的树,不一定是树有可能有独立的点,但是这题查询的时候应该都是合法的。q次查询,求x,y的距离。

    思路:

    求出x,y的lca,然后减一减~~(具体看上篇)。

    /*
     * Author:  sweat123
     * Created Time:  2016/7/13 10:27:16
     * File Name: main.cpp
     */
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<time.h>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1<<30
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define key_value ch[ch[root][1]][0]
    #define rson m+1,r,rt<<1|1
    #define pi acos(-1.0)
    using namespace std;
    const int MAXN = 40010;
    struct node{
        int to;
        int val;
        int next;
    }edge[MAXN*2];
    int dfn[MAXN*2],dis[MAXN],ver[MAXN*2],vis[MAXN],dp[MAXN*2][20],first[MAXN],n,pre[MAXN],ind,m,tot;
    void add(int x,int y,int z){
        edge[ind].to = y;
        edge[ind].val = z;
        edge[ind].next = pre[x];
        pre[x] = ind ++;
    }
    void dfs(int rt,int dep){
        vis[rt] = 1;
        ver[++tot] = rt;
        dfn[tot] = dep;
        first[rt] = tot;
        for(int i = pre[rt]; i != -1; i = edge[i].next){
            int t = edge[i].to;
            if(!vis[t]){
                dis[t] = dis[rt] + edge[i].val;
                dfs(t,dep+1);
                ver[++tot] = rt;
                dfn[tot] = dep;
            }
        }
    }
    void rmq(){
        for(int i = 1; i <= tot; i++){
            dp[i][0] = i;
        }
        for(int i = 1; i < 20; i++){
            for(int j = 1; j + (1 << i) - 1 <= tot; j++){
                int x = dp[j][i-1];
                int y = dp[j + (1<<(i-1))][i-1];
                if(dfn[x] > dfn[y]){
                    dp[j][i] = y;
                } else {
                    dp[j][i] = x;
                }
            }
        }
    }
    int askrmq(int x,int y){
        x = first[x];
        y = first[y];
        if(x > y)swap(x,y);
        int k = (int)(log(y - x + 1) * 1.0 / log(2.0));
        int l = dp[x][k];
        int r = dp[y-(1<<k)+1][k];
        if(dfn[l] > dfn[r])return r;
        else return l;
    }
    int main(){
        while(~scanf("%d%d",&n,&m)){
            ind = tot = 0;
            memset(vis,0,sizeof(vis));
            memset(pre,-1,sizeof(pre));
            for(int i = 1; i <= m; i++){
                int x,y,z;
                char s[10];
                scanf("%d%d%d%s",&x,&y,&z,s);
                add(x,y,z);
                add(y,x,z);
            }
            memset(dis,0,sizeof(dis));
            dfs(1,1);
            rmq();
            int q;
            scanf("%d",&q);
            while(q--){
                int x,y;
                scanf("%d%d",&x,&y);
                int tp = ver[askrmq(x,y)];
                int ans = dis[x] - dis[tp] + dis[y] - dis[tp];
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    XP的定时关机命令?
    Vim简明教程【CoolShell】
    我和小美的撸码日记(3)之中的一个句话搞定MVC表单页数据绑定与提交
    Linux/RedHat 编译安装GNU gcc 4.9.0 (g++)
    真实股市房市信托本质,金融故事三则:钱都去哪儿了?
    一分钟制作U盘版BT3
    Android Studio中导入第三方库
    WebService究竟是什么?
    “权限”用英语该用哪个?
    Cocos2d-x v3.0 新的事件调度方法 lambda表达式的使用
  • 原文地址:https://www.cnblogs.com/sweat123/p/5666091.html
Copyright © 2011-2022 走看看