1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 7 const int MAX = 100010; 8 const int MAXN = 2 * MAX; 9 const int MAXE = 2 * MAXN; 10 11 int head[MAXN], weight[MAXN], fa[MAX]; 12 bool vis[MAX]; 13 int to[MAXE], next[MAXE], id[MAXE]; 14 int n, m, ecnt; 15 16 inline void init() { 17 memset(head, 0, sizeof(head)); 18 memset(vis, 0, sizeof(vis)); 19 for(int i = 1; i <= n; ++i) fa[i] = i; 20 ecnt = 2; 21 } 22 23 inline void add_edge(int u, int v, int i) { 24 to[ecnt] = v; id[ecnt] = i; next[ecnt] = head[u]; head[u] = ecnt++; 25 to[ecnt] = u; id[ecnt] = i; next[ecnt] = head[v]; head[v] = ecnt++; 26 } 27 28 struct QUERY { 29 int x, y, lca, z; 30 void read(int i) { 31 scanf("%d%d%d", &x, &y, &z); 32 add_edge(x + n, y + n, i); 33 } 34 } Query[MAX]; 35 36 int get_set(int x) { 37 return fa[x] == x ? x : fa[x] = get_set(fa[x]); 38 } 39 40 void dfs_LCA(int u, int f) { 41 for(int p = head[u]; p; p = next[p]) { 42 int &v = to[p]; 43 if(v == f) continue; 44 dfs_LCA(v, u); 45 fa[v] = u; 46 } 47 vis[u] = true; 48 for(int p = head[u + n]; p; p = next[p]) { 49 int v = to[p] - n; 50 if(vis[v]) Query[id[p]].lca = get_set(v); 51 } 52 } 53 54 struct Node { 55 int go[2], cnt; 56 } tree[20 * MAX]; 57 int root[MAX], Tcnt; 58 59 void init_tree() { 60 Tcnt = 1; root[0] = 0; 61 } 62 63 int insert(int x, int val) { 64 tree[Tcnt] = tree[x]; 65 int ret = x = Tcnt++; 66 for(int i = 15; i >= 0; --i) { 67 int idx = (val >> i) & 1, t = Tcnt++; 68 tree[t] = tree[tree[x].go[idx]]; 69 ++tree[t].cnt; 70 tree[x].go[idx] = t; 71 x = t; 72 } 73 return ret; 74 } 75 76 void dfs_build(int u, int f) { 77 root[u] = insert(root[f], weight[u]); 78 for(int p = head[u]; p; p = next[p]) { 79 int &v = to[p]; 80 if(v == f) continue; 81 dfs_build(v, u); 82 } 83 } 84 85 int query(int x, int y, int lca, int val) { 86 int ret = 0, ret_lca = weight[lca] ^ val; 87 x = root[x], y = root[y], lca = root[lca]; 88 for(int i = 15; i >= 0; --i) { 89 int idx = !((val >> i) & 1); 90 int cnt = tree[tree[x].go[idx]].cnt + tree[tree[y].go[idx]].cnt - 2 * tree[tree[lca].go[idx]].cnt; 91 if(cnt > 0) ret |= (1 << i); 92 else idx = !idx; 93 x = tree[x].go[idx], y = tree[y].go[idx], lca = tree[lca].go[idx]; 94 } 95 return max(ret, ret_lca); 96 } 97 98 int main() { 99 while(scanf("%d%d", &n, &m) != EOF) { 100 for(int i = 1; i <= n; ++i) scanf("%d", &weight[i]); 101 init(); 102 for(int i = 1; i < n; ++i) { 103 int u, v; 104 scanf("%d%d", &u, &v); 105 add_edge(u, v, 0); 106 } 107 for(int i = 1; i <= m; ++i) Query[i].read(i); 108 dfs_LCA(1, 0); 109 init_tree(); 110 dfs_build(1, 0); 111 for(int i = 1; i <= m; ++i) 112 printf("%d\n", query(Query[i].x, Query[i].y, Query[i].lca, Query[i].z)); 113 } 114 }