/*
ID: varunra2
LANG: C++
TASK: milkvisits
*/
// test case failing:
//29945 81162 5
//line number 100144
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include <debug.h>
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// util functions
// segtree impl
//qry: contains int x in range l ... r
const int MXN = 100000;
int n, m;
VVI segtree(4*MXN);
void segbuild(VI& a, int vert, int left, int right) {
if(left == right) {
segtree[vert] = VI (1, a[left]);
}
else {
int mid = left + right;
mid /= 2;
//debug (vert);
segbuild(a, 2*vert, left, mid);
segbuild(a, 2*vert + 1, mid + 1, right);
merge(segtree[2*vert].begin(), segtree[2*vert].end(), segtree[2*vert + 1].begin(),
segtree[2*vert + 1].end(), back_inserter(segtree[vert]));
}
}
void segbuild(VI& a) {
segbuild(a, 1, 0, n - 1);
}
int segqry(int vert, int left, int right, int l, int r, int x) {
if(l > r) return INF;
if(right < l || left > r) return INF;
if(left >= l && right <= r) {
auto it = lower_bound(segtree[vert].begin(), segtree[vert].end(), x);
if(it != segtree[vert].end()) return *it;
return INF;
}
int mid = left + right;
mid /= 2;
int ret = min(segqry(2*vert, left, mid, l, r, x),
segqry(2*vert + 1, mid + 1, right, l, r, x));
//debug (vert, left, right, l, r, ret);
return ret;
}
int segqry(int l, int r, int x) {
return segqry(1, 0, n - 1, l, r, x);
}
//hld impl
const int MXD = 20;
//lca impl w/ binary lifting
VVI lca(MXN, VI(MXD, 0));
VI dist(MXN);
void setLCA() {
for(int j = 1; j < MXD; j++) {
for(int i = 0; i < n; i++) {
lca[i][j] = lca[lca[i][j - 1]][j - 1];
}
}
}
int getLCA(int u, int v) {
if(dist[u] < dist[v]) swap(u, v);
for(int j = MXD - 1; j >= 0; j--) {
if(dist[u] - (1 << j) >= dist[v]) {
u = lca[u][j];
}
}
for(int j = MXD - 1; j >= 0; j--) {
if(lca[u][j] != lca[v][j]) {
u = lca[u][j];
v = lca[v][j];
}
}
if(u != v) {
return lca[u][0];
}
return u;
}
VVI adj(MXN);
VI subtreesz(MXN, 0);
VI vertsegtree(MXN);
VI topseg(MXN);
VI vals(MXN);
void firstinit(int x) {
segtree.resize(4*x);
lca.resize(x);
adj.resize(x);
subtreesz.resize(x);
vertsegtree.resize(x);
topseg.resize(x);
vals.resize(x);
dist.resize(x);
}
void dfsinits(int ind, int par) {
subtreesz[ind]++;
for(auto& x: adj[ind]) {
if(x == par) continue;
dist[x] = dist[ind] + 1;
lca[x][0] = ind;
dfsinits(x, ind);
subtreesz[ind] += subtreesz[x];
}
}
void dfsmapping(int pos, int top, int par, int& curind) {
vertsegtree[pos] = curind++;
topseg[pos] = top;
int mxsiz = -1;
int mxchild = -1;
for(auto& x: adj[pos]) {
if(x == par) continue;
if(subtreesz[x] > mxsiz) {
mxsiz = subtreesz[x];
mxchild = x;
}
}
if(mxchild == -1) {
return;
}
dfsmapping(mxchild, top, pos, curind);
for(auto& x: adj[pos]) {
if(x == par) continue;
if(x == mxchild) continue;
dfsmapping(x, x, pos, curind);
}
}
void initHLD() {
dfsinits(0, -1);
setLCA();
int curind = 0;
dfsmapping(0, 0, -1, curind);
}
bool pqry(int u, int v, int x) {
int ret = INF;
if(u == v) {
ret = vals[u];
}
while(u != v) {
if(topseg[u] == u) {
if(vals[u] >= x) ret = min(ret, vals[u]); // this is light edge so we are switching to heavy
//debug("light ", u);
u = lca[u][0]; // getting to parent
}
else if(dist[topseg[u]] > dist[v]) {
ret = min(ret, segqry(vertsegtree[topseg[u]], vertsegtree[u], x));
u = lca[topseg[u]][0];
//debug("heavy1 ", u);
}
else {
ret = min(ret, segqry(vertsegtree[v], vertsegtree[u], x));
break;
}
debug(ret);
}
return ret == x;
}
int qry(int u, int v, int x) {
int l = getLCA(u, v);
debug(l);
//debug (u, v, l);
if(pqry(u, l, x) || pqry(v, l, x)) return 1;
return 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("milkvisits.in", "r", stdin);
freopen("milkvisits.out", "w", stdout);
#endif
cin.sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
firstinit(n);
for(int i = 0; i < n; i++) {
cin >> vals[i];
}
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
adj[u].PB(v);
adj[v].PB(u);
}
initHLD();
VI reorder(n);
for(int i = 0; i < n; i++) {
reorder[vertsegtree[i]] = vals[i];
}
VI inds;
for(int i = 0; i < n; i++) {
if(vals[i] == 5) inds.PB(i);
}
debug(inds);
//debug (vals);
//debug (reorder);
debug (lca[29944]);
debug (lca[81161]);
//debug (dist);
//debug(topseg);
//debug(vertsegtree);
segbuild(reorder);
for(int i = 0; i < m; i++) {
int u, v, x;
cin >> u >> v >> x;
u--;
v--;
if (v > u) swap(u, v);
cout << qry(u, v, x);
}
cout << endl;
return 0;
}
LyoKSUQ6IHZhcnVucmEyCkxBTkc6IEMrKwpUQVNLOiBtaWxrdmlzaXRzCiovCi8vIHRlc3QgY2FzZSBmYWlsaW5nOiAKLy8yOTk0NSA4MTE2MiA1Ci8vbGluZSBudW1iZXIgMTAwMTQ0CgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZD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