#include #include #include #include using namespace std; const int maxn = 1e5 + 10; vector graph[maxn]; int main() { ifstream cin("milkvisits.in"); ofstream cout("milkvisits.out"); int n, m; cin >> n >> m; string s; cin >> s; for(int i = 1; i < n; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } vector, char>> queries; for(int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; char c; cin >> c; queries.push_back(make_pair(make_pair(a, b), c)); } vector id(n, -1); for(int i = 0; i < n; i++) { if(id[i] == -1) { queue q; q.push(i); while(!q.empty()) { int node = q.front(); q.pop(); id[node] = i; for(int neighbour : graph[node]) { if(id[neighbour] == -1 and s[neighbour] == s[i]) { q.push(neighbour); } } } } } string res = ""; for(int i = 0; i < m; i++) { int a = queries[i].first.first; int b = queries[i].first.second; char c = queries[i].second; if(id[a] == id[b]) { if(s[a] == c) { res += "1"; } else { res += "0"; } } else { res += "1"; } } cout << res << endl; return 0; }