题解:
更据题意,在树上深度为没一个数的都放在一起,要用的时候二分出来,看结果
用c++的数据结构
代码:
#include<bits/stdc++.h> using namespace std; const int N=5e5+10,L=27; char s[N]; int n,m,cnt=0,max_h=0,high[N]; vector<int>E[N],A[N],B[N]; pair<int,int>order[N]; void dfs(int x,int h) { max_h=max(h,max_h); high[x]=h; order[x].first=++cnt; A[h].push_back(1<<(s[x-1]-'a')); B[h].push_back(cnt); for (int i=0;i<E[x].size();i++)dfs(E[x][i],h+1); order[x].second=cnt; } int solve(int x,int h) { if (h<=high[x]||!B[h].size())return 1; int l=(int)(lower_bound(B[h].begin(),B[h].end(), order[x].first)-B[h].begin()); int r=(int)(upper_bound(B[h].begin(),B[h].end(), order[x].second)-B[h].begin()-1); if (l>r)return 1; int ans=A[h][r]^(l?A[h][l-1]:0); if (ans==(ans&(-ans)))return 1; return 0; } int read() { char c=0;int x=0; for(;c<'0'||c>'9';c=getchar()); for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0'; return x; } int main() { n=read();m=read(); for (int i=2;i<=n;i++)E[read()].push_back(i); scanf("%s",&s); dfs(1,0); max_h++; for (int i=0;i<max_h;i++) for (int j=1;j<A[i].size();j++)A[i][j]^=A[i][j-1]; while (m--) { int v=read(),h=read(); if (solve(v,h-1))puts("Yes"); else puts("No"); } }