传送门
题意:
一棵树,询问某棵子树指定深度的点能否构成回文
当然不用dsu on tree也可以做
dsu on tree的话,维护当前每一个深度每种字母出现次数和字母数,我直接用了二进制....
一开始dfs没有判断重儿子T了一次
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
#define pii pair<int, ll>
#define MP make_pair
#define fir first
#define sec second
const int N=5e5+5;
int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
return x*f;
}
int n, a[N], Q, x;
char s[N];
vector<pii> q[N];
int ans[N];
struct edge{int v, ne;}e[N<<1];
int cnt, h[N];
inline void ins(int u, int v) {
e[++cnt]=(edge){v, h[u]}; h[u]=cnt;
}
int size[N], mx[N], deep[N], big[N];
void dfs(int u) {
size[u]=1;
for(int i=h[u];i;i=e[i].ne) {
deep[e[i].v] = deep[u]+1;
dfs(e[i].v);
size[u] += size[e[i].v];
if(size[e[i].v] > size[mx[u]]) mx[u] = e[i].v;
}
}
pii f[N];
void update(int u, int val) {
f[ deep[u] ].fir += val;
f[ deep[u] ].sec ^= (1<<(a[u]));
for(int i=h[u];i;i=e[i].ne) if(!big[e[i].v]) update(e[i].v, val);
}
inline int one(int n) {
int ans=0;
while(n) n&=(n-1), ans++;
return ans;
}
inline int cal(int d) {return f[d].sec == 0 || (one(f[d].sec) == 1 && (f[d].fir & 1) );}
void dfs(int u, int keep) {
for(int i=h[u];i;i=e[i].ne)
if(e[i].v != mx[u]) dfs(e[i].v, 0);
if(mx[u]) dfs(mx[u], 1), big[mx[u]]=1;
update(u, 1);
for(int i=0; i<(int)q[u].size(); i++) ans[q[u][i].sec] = cal(q[u][i].fir);
big[mx[u]]=0;
if(!keep) update(u, -1);
}
int main() {
//freopen("in","r",stdin);
n=read(); Q=read();
for(int i=2; i<=n; i++) ins(read(), i);
scanf("%s",s+1);
for(int i=1; i<=n; i++) a[i] = s[i]-'a';
for(int i=1; i<=Q; i++) x=read(), q[x].push_back(MP(read(), i));
deep[1] = 1; dfs(1);
dfs(1, 1);
for(int i=1; i<=Q; i++) puts(ans[i] ? "Yes" : "No");
}