前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战程序竞赛系列(82):4.3 LCA(2)

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

作者头像
用户1147447
发布2018-01-02 11:04:30
4230
发布2018-01-02 11:04:30
举报
文章被收录于专栏:机器学习入门

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

传送门:POJ 1986: Distance Queries

题意:

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

思路:

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

代码如下:

代码语言:javascript
复制
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);
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 挑战程序竞赛系列(82):4.3 LCA(2)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档