#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, N2 = 20 + 5;
int n, m, dep[N], fa[N][N2], h[N][N2], g[N][N2];
string s;
vector<int> adj[N];
void dfs(int u, int lst) {
for (int j = 1; j <= 20; ++j) {
fa[u][j] = fa[fa[u][j - 1]][j - 1];
g[u][j] = g[u][j - 1] | g[fa[u][j - 1]][j - 1];
h[u][j] = h[u][j - 1] | h[fa[u][j - 1]][j - 1];
}
for (int i = 0; i < adj[u].size(); ++i) {
int v = adj[u][i];
if (v == lst) continue;
dep[v] = dep[u] + 1;
fa[v][0] = u;
if (s[v] == 'H' || s[u] == 'H') h[v][0] = 1;
if (s[v] == 'G' || s[u] == 'G') g[v][0] = 1;
dfs(v, u);
}
}
int check(int x, int y, char c) {
if (x == y) {
if (c == 'H' && s[x] == 'H') return 1;
if (c == 'G' && s[x] == 'G') return 1;
return 0;
}
int ok = 0, ok2 = 0;
if (dep[x] < dep[y]) swap(x, y);
for (int i = 20; i >= 0; --i) {
if (dep[fa[x][i]] >= dep[y]) {
ok |= g[x][i];
ok2 |= h[x][i];
x = fa[x][i];
}
}
if (x == y) {
if (c == 'H' && ok2) return 1;
if (c == 'G' && ok) return 1;
return 0;
}
for (int i = 20; i >= 0; --i) {
if (fa[x][i] != fa[y][i]) {
ok |= g[x][i], ok |= g[y][i];
ok2 |= h[x][i], ok2 |= h[y][i];
x = fa[x][i];
y = fa[y][i];
}
}
ok |= g[x][0];
ok2 |= h[y][0];
if (c == 'H' && ok2) return 1;
if (c == 'G' && ok) return 1;
return 0;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> s;
s = "." + s;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dep[1] = 1;
if (s[1] == 'H') h[1][0] = 1;
if (s[1] == 'G') g[1][0] = 1;
dfs(1, -1);
while (m--) {
int x, y;
char c;
cin >> x >> y >> c;
cout << check(x, y, c);
}
cout << endl;
return 0;
}
昨天晚上发了一遍,结果每人理我,救救蒟蒻……