挑战程序竞赛系列(82):4.3 LCA(2)

挑战程序竞赛系列(82):4.3 LCA(2)

传送门:POJ 1986: Distance Queries

题意:

LCA距离:快速查询树中任意两个节点间的最短距离。

思路: 树中,每两个结点的路径是唯一的,比POJ: 2763简单,稍微改改就过了,思路一致,解题套路。

代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201709/P1986.txt";

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    int N, M;
    int root;         // 选定树的任意一个根
    List<Edge>[] g;   // 树的邻接表

    int[] id;         // 记录每个结点最先访问的时间戳
    int[] vs;         // dfs访问顺序
    int[] dp;         // 访问过程中对应结点的深度

    void init(int n) {
        g = new ArrayList[n];
        for (int i = 0; i < n; ++i) g[i] = new ArrayList<Edge>();

        id = new int[n * 2];
        vs = new int[n * 2];
        dp = new int[n * 2];
    }

    void add(int from, int to, int cost) {
        g[from].add(new Edge(to, cost));
        g[to].add(new Edge(from, cost));
    }

    class Edge{
        int to;
        int cost;
        Edge(int to, int cost){
            this.to   = to;
            this.cost = cost;
        }
    }

    class RMQ {
        int n_;
        int[] dat;
        int[] data;


        RMQ (int N) {
            n_ = 1;
            while (n_ < N) n_ *= 2;
            dat = new int[2 * n_];
            for (int i = 0; i < 2 * n_ - 1; ++i) dat[k] = -1;
        }

        RMQ (int[] data, int N) {
            this(N);
            this.data = data;
            for (int i = 0; i < N; ++i) {
                update(i, i);
            }
        }

        void update(int k, int i) {
            k += (n_ - 1);
            dat[k] = i;
            while (k > 0) {
                k = (k - 1) / 2;
                dat[k] = min(dat[2 * k + 1], dat[2 * k + 2]);
            }
        }

        int min(int i, int j) { // -1表示无穷大 
            if (i == -1 && j != -1) return j;
            if (j == -1 && i != -1) return i;
            if (j == -1 && i == -1) return -1;
            return data[i] < data[j] ? i : j;
        }

        int query(int k, int i, int j, int l, int r) {
            if (j <= l || i >= r) return -1;
            else if (i <= l && j >= r) {
                return dat[k];
            }
            else {
                int lch = 2 * k + 1;
                int rch = 2 * k + 2;
                int mid = (l + r) / 2;
                int lf = query(lch, i, j, l, mid);
                int rt = query(rch, i, j, mid, r);
                return min(lf, rt);
            }
        }
    }

    class BIT{
        int n;
        int[] BIT;

        BIT(int n){
            this.n = n;
            BIT = new int[n + 16];
        }

        void add(int i, int val) {
            while (i <= n) {
                BIT[i] += val;
                i += i & -i;
            }
        }

        long sum(int i) {
            long s = 0;
            while (i > 0) {
                s += BIT[i];
                i -= i & -i;
            }
            return s;
        }
    }

    BIT bit;
    RMQ rmq;
    void solve() {
        bit = new BIT(2 * N);
        k = 0;
        dfs(root, -1, 0);
        rmq = new RMQ(dp, 2 * N);
    }

    int k = 0;
    void dfs(int v, int p, int d) {
        id[v] = k;
        vs[k] = v;
        dp[k++] = d;
        for (Edge e : g[v]) {
            if (e.to != p) {
                bit.add(k, e.cost);
                dfs(e.to, v, d + 1);
                vs[k] = v;
                bit.add(k, -e.cost);
                dp[k++] = d;
            }
        }
    }

    int lca(int u, int v) {
        return vs[rmq.query(0, Math.min(id[u], id[v]), Math.max(id[u], id[v]) + 1, 0, rmq.n_)];
    }

    void read() {
        N = ni();
        M = ni();
        root = N / 2;

        init(N);

        for (int i = 0; i < M; ++i) {
            int from = ni();
            int to   = ni();
            int cost = ni();
            char r   = nc();
            from --;
            to   --;
            add(from, to, cost);
        }

        solve();

        int q = ni();
        for (int i = 0; i < q; ++i) {
            int u = ni();
            int v = ni();
            u --;
            v --;
            int p = lca(u, v);
            long ans = bit.sum(id[u]) + bit.sum(id[v]) - 2 * bit.sum(id[p]);
            out.println(ans);
        }
    }

    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏机器学习入门

挑战程序竞赛系列(70):4.7后缀数组(2)

挑战程序竞赛系列(70):4.7后缀数组(2) 传送门:POJ 1509: Glass Beads 题意: The description of the ne...

1967
来自专栏

Cassandra API60 Java 代码示例

文档地址 http://wiki.apache.org/cassandra/API06,实现了绝大部分示例

1434
来自专栏机器学习入门

挑战程序竞赛系列(91):3.6凸包(2)

挑战程序竞赛系列(91):3.6凸包(2) 传送门:POJ 1113: Wall 题意参考hankcs: http://www.hankcs.com/pro...

1926
来自专栏GIS讲堂

geotools中等值面的生成与OL3中的展示

本文讲述如何在geotools中IDW插值生成等值面,并根据给定shp进行裁剪,并生成geojson数据,以及Openlayers3中展示。

865
来自专栏机器学习入门

挑战程序竞赛系列(76):4.3强连通分量分解(3)

挑战程序竞赛系列(76):4.3强连通分量分解(3) 传送门:POJ 1236: Network of Schools 题意: 校园网:给定N所学校和网络,目...

1695
来自专栏机器学习入门

挑战程序竞赛系列(94):3.6凸包(5)

挑战程序竞赛系列(94):3.6凸包(5) 传送门:POJ 2079: Triangle 题意: 求三个点构成的最大三角形面积。 思路: 可以证明,三点构...

1669
来自专栏ml

数据挖掘之聚类算法Apriori总结

项目中有时候需要用到对数据进行关联分析,比如分析一个小商店中顾客购买习惯. 1 package com.data.algorithm; 2 3 i...

3527
来自专栏机器学习入门

挑战程序竞赛系列(93):3.6凸包(4)

挑战程序竞赛系列(93):3.6凸包(4) 传送门:POJ 3608: Bridge Across Islands 题意: 跨岛大桥:在两个凸包小岛之间造桥,...

1996
来自专栏闻道于事

SSM框架整合项目 :投票系统

框架: Spring SpringMVC MyBatis 题目: 投票系统 ? 导包: 1, spring 2, MyBatis 3, mybatis-spri...

6425
来自专栏Hongten

Hibernate的性能优化之 dynamic-update dynamic-insert

public class Guestbook implements java.io.Serializable {

692

扫码关注云+社区