#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 3;
int head[N], heavy[N], depth[N], n, m, a[N], par[N], sz[N], t, pos[N];
vector<int> g[N], mang[N];
void DFS(int u){
    sz[u] = 1;
    int maxx = 0;
    for(auto x: g[u]){
        if(x == par[u]) continue;
        depth[x] = depth[u] + 1;
        par[x] = u;
        DFS(x);
        sz[u] += sz[x];
        if(sz[x] > maxx){
            maxx= sz[x];
            heavy[u] = x;
        }
    }
    return;
}
void buildhld(int u, int h){
    head[u] = h;
    pos[u] = ++t;
    if(heavy[u]){
        buildhld(heavy[u], h);
    }
    for(auto x: g[u]){
        if(x == par[u] || x == heavy[u]) continue;
        buildhld(x, x);
    }
    return;
}
bool solve(int u, int v, int val){
    int res = 0;
    while(head[u] != head[v]){
        if(depth[head[u]] > depth[head[v]]) swap(u, v);
        res += upper_bound(mang[val].begin(), mang[val].end(), pos[v])
        - lower_bound(mang[val].begin(), mang[val].end(), pos[head[v]]);
        v = par[head[v]];
    }
    if(depth[u] > depth[v]) swap(u, v);
    res += upper_bound(mang[val].begin(), mang[val].end(), pos[v])
        - lower_bound(mang[val].begin(), mang[val].end(), pos[u]);
    return res > 0;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int u, v, i, w;
    cin >> n >> m;
    for(i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(i = 1; i < n; i++){
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    DFS(1);
    buildhld(1, 1);
    for(i = 1; i <= n; i++){
        mang[a[i]].push_back(pos[i]);
    }
    for(i = 1; i <= n; i++) sort(mang[i].begin(), mang[i].end());
    for(i = 1; i <= m; i++){
        cin >> u >> v >> w;
        cout << solve(u, v, w);
    }
    return 0;
}
