1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| #include<bits/stdc++.h> using namespace std; #define int long long const int N = 500010; typedef pair<int, int> pii;
int read() { int x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }
int h[N], idx, n, dep[N], sz[N], son[N], m, cnt[N][26], res[N]; string s; struct Rec{ int k,id; }; std::vector<Rec>q[N]; struct E{ int to, ne; } e[N];
void add (int a, int b) { e[idx].to = b; e[idx].ne = h[a]; h[a] = idx++; }
bool check (int d) { int res = 0; for(int i = 0; i < 26; i++) { res += (cnt[d][i] & 1); } return res <= 1; }
void dfs(int x, int fa){ sz[x]=1, dep[x] = dep[fa] + 1; for(int i=h[x], v; ~i; i = e[i].ne){ v = e[i].to; if(v == fa) continue; dfs(v,x), sz[x] += sz[v]; if(!son[x] || sz[v] > sz[son[x]]) son[x] = v; } } void calc(int x,int fa,int val, int pson){ cnt[dep[x]][s[x]-'a']+=val; for(int i=h[x], v; ~i; i = e[i].ne){ v=e[i].to; if(v == fa || v == pson) continue; calc(v,x,val, pson); } }
void dfs(int x,int fa,int keep){ for(int i=h[x], v; ~i; i = e[i].ne){ v = e[i].to; if(v ==fa || v == son[x]) continue; dfs(v, x, 0); } if(son[x]) dfs(son[x], x, 1); calc(x,fa,1, son[x]); for(int i=0; i < q[x].size(); i++) res[q[x][i].id] = check(q[x][i].k); if(!keep) calc(x,fa,-1, 0); }
signed main () { memset(h, -1, sizeof h); cin >> n >> m; for(int i = 2; i <= n; i++) { int x = read(); add(x, i); } dfs(1, 0); cin >> s; s = "?" + s; for(int i = 1; i <= m; i++) { int v = read(), h = read(); q[v].push_back({h, i}); } dfs(1, 0, false); for(int i = 1;i <= m; i++ ) puts(res[i] ? "Yes" : "No"); return 0; }
|