//USACO 2019 December Contest, Gold
//Problem 2. Milk Visits
#include<bits/stdc++.h>
#define int long long
using namespace std;
void usaco(){
freopen("milkvisits.in","r",stdin);
freopen("milkvisits.out","w",stdout);
}
const int N=1e5+5;
vector<int> a[N];
int t[N],ans[N],an[N][21],dep[N],n,m;
vector<tuple<int,int,int>>q[N];
set<int> c[N];
void build(int x,int p){
an[x][0]=p;
for(int j=1;j<21;j++) an[x][j]=an[an[x][j-1]][j-1];
dep[x]=dep[p]+1;
for(auto i:a[x]) if(i!=p) build(i,x);
}
int lca(int x,int y){
for(int i=20;i>=0;i--){
int u=an[y][i];
if(dep[u]>=dep[x]) y=u;
}
if(x==y) return x;
for(int i=20;i>=0;i--){
if(an[x][i]!=an[y][i]){
x=an[x][i]; y=an[y][i];
}
}
return an[x][0];
}
void dfs(int x,int p){
c[t[x]].insert(dep[x]);
for(auto [d,tt,i]:q[x]){
if(c[tt].size()&&*c[tt].rbegin()>=d) ans[i]=1;
}
for(auto i:a[x]){
if(i!=p) dfs(i,x);
}
c[t[x]].erase(dep[x]);
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
usaco();
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>t[i];
for(int i=1;i<n;i++){
int u,v; cin>>u>>v;
a[u].push_back(v);
a[v].push_back(u);
}
build(1,0);
for(int i=0;i<m;i++){
int u,v,tt; cin>>u>>v>>tt;
if(dep[u]>dep[v]) swap(u,v);
int g=lca(u,v);
if(g==u) q[v].push_back({dep[u],tt,i});
else {
q[u].push_back({dep[g],tt,i});
q[v].push_back({dep[g],tt,i});
}
}
dfs(1,0);
for(int i=0;i<m;i++) cout<<ans[i];
}
Ly9VU0FDTyAyMDE5IERlY2VtYmVyIENvbnRlc3QsIEdvbGQKLy9Qcm9ibGVtIDIuIE1pbGsgVmlzaXRzCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgaW50IGxvbmcgbG9uZwp1c2luZyBuYW1lc3BhY2Ugc3RkOwp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