POJ3764 The xor-longest Path

原题链接:POJ3764

题目大意:给定一个带权图,求两点间最长异或值路径。

Trie

设 $ f(a,b) $ 为 $ a $ 点到 $ b $ 点路径异或和,$ root $ 为树根,则 $ f(a, b) = $$f(a, root)$ $\bigoplus$$f(b, root)$

用 $d[N]$ 维护每个点到根的路径异或和,从根 $\rm DFS$ 一遍,得到 $ d[N] $,最后插入 $\rm Trie$ 树,最后转化成最大异或对问题。

 1
 2
 3
 4
 5
 6
 7
 8
 9
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 100010, M = 2 * N, K = 31 * N;

int n;
int h[N], e[M], w[M], ne[M], idx;
int son[K][2], cnt;
int d[N]; // f(a, b) = f(a, root) ^ f(b, root)

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void insert(int x) {
    int p = 0;
    for (int i = 30; ~i; i--) {
        int u = x >> i & 1;
        if (!son[p][u]) son[p][u] = ++idx;
        p = son[p][u];
    }
}

int query(int x) {
    int p = 0, res = 0;
    for (int i = 30; ~i; i--) {
        int u = x >> i & 1;
        if (son[p][!u]) {
            res += 1 << i;
            p = son[p][!u];
        }
        else p = son[p][u];
    }
    return res;
}

void init() {
    memset(h, -1, sizeof h);
    memset(ne, 0, sizeof ne);
    idx = cnt = 0;
    memset(d, 0, sizeof d);
    memset(son, 0, sizeof son);
}

void dfs(int u, int father, int sum) {
    d[u] = sum;
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j != father) dfs(j, u, sum ^ w[i]);
    }
}

int main() {
    while (cin >> n) {
        init();
        for (int i = 0; i < n - 1; i++) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c), add(b, a, c);
        }
        dfs(0, -1, 0);
        for (int i = 0; i < n; i++) insert(d[i]);
        int res = 0;
        for (int i = 0; i < n; i++) res = max(res, query(d[i]));
        cout << res << endl;
    }
    return 0;
}
updatedupdated2020-05-192020-05-19
加载评论
点击刷新