Tarjan求LCA.java 3.5 KB
Newer Older
qq_36480062's avatar
c  
qq_36480062 已提交
1 2 3 4 5 6 7 8
package LCA;

import java.io.*;
import java.util.StringTokenizer;

import static java.lang.System.in;

/**
qq_36480062's avatar
c  
qq_36480062 已提交
9
 * https://www.cnblogs.com/hulean/p/11144059.html
qq_36480062's avatar
c  
qq_36480062 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 * 离线
 * 预处理O(n)
 * 单次查询O(1)
 * 1
 * 1
 * 2
 * 2
 * 1
 * 4
 * 7
 * 3
 * 洛谷3379 tel2个
 * 最好的版本!!!
 */
public class Tarjan求LCA {
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException {// 读取下一行字符串
        return reader.readLine();
    }

    static String next() throws IOException {// 读取下一个字符串
        while (!tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {// 读取下一个int型数值
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {// 读取下一个double型数值
        return Double.parseDouble(next());
    }

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        root = nextInt();
        for (int i = 1; i < n; i++) {
            add(nextInt(), nextInt());
        }
        for (int i = 0; i < m; i++) {
            addq(nextInt(), nextInt(), i);
qq_36480062's avatar
c  
qq_36480062 已提交
57
        }//存储询问
qq_36480062's avatar
c  
qq_36480062 已提交
58
        tarjan(root, 0);
qq_36480062's avatar
c  
qq_36480062 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        for (int i = 0; i < m; i++) {
            bw.write(result[i] + "\n");
        }
        bw.flush();
    }

    static int maxn = 500001;
    static int[] e = new int[maxn << 1];
    static int[] he = new int[maxn];
    static int[] ne = new int[maxn << 1];
    static int[] parent = new int[maxn];
    static int cnt = 1;
    static int[] qheadedge = new int[maxn];
    static int[] qto = new int[maxn << 1];
    static int[] qnext = new int[maxn << 1];
    static int[] qindex = new int[maxn << 1];
    static int[] result = new int[maxn];
    static int qidx = 1;
    static boolean[] vis = new boolean[maxn];
    static int n, m, root;

    static void add(int a, int b) {
        e[cnt] = a;
        ne[cnt] = he[b];
        he[b] = cnt++;
        e[cnt] = b;
        ne[cnt] = he[a];
        he[a] = cnt++;
    }

    static void addq(int a, int b, int i) {
        qto[qidx] = a;
        qindex[qidx] = i;
        qnext[qidx] = qheadedge[b];
        qheadedge[b] = qidx++;
        qto[qidx] = b;
        qindex[qidx] = i;
        qnext[qidx] = qheadedge[a];
        qheadedge[a] = qidx++;
    }

    static {
        for (int i = 0; i < parent.length; i++) {
            parent[i] = i;
        }
    }


qq_36480062's avatar
c  
qq_36480062 已提交
107
    static void tarjan(int u, int fa) {
qq_36480062's avatar
c  
qq_36480062 已提交
108 109 110
        //vis[u] = true;
        for (int i = he[u]; i != 0; i = ne[i]) {
            if (e[i] != fa) {
qq_36480062's avatar
c  
qq_36480062 已提交
111
                tarjan(e[i], u);
qq_36480062's avatar
c  
qq_36480062 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                union(e[i], u);
                vis[e[i]] = true;
            }
        }
        //  vis[u] = true;
        for (int i = qheadedge[u]; i != 0; i = qnext[i]) {
            int j = qto[i];
            if (vis[j]) {
                result[qindex[i]] = find(j);
                //放到结果里
            }
        }
    }

    static int find(int x) {
        while (x != parent[x]) {
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        return x;
    }

    static void union(int v, int u) {
        v = find(v);
        u = find(u);
        if (u != v) parent[v] = u;
    }

}