Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF570D Tree Requests

LuoGu: CF570D Tree Requests

CF: D. Tree Requests

构成回文串当且仅当出现次数为奇数的字母不超过 $1$ ,裸的长剖。

查看代码
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
#include <cstdio>
#include <vector>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...rest>
void read(Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write(Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
const int N = 5e5 + 10;
bool ans[N];
int v[N], *cur = v, *f[N];
int n, m, w[N], s[N], d[N], son[N];
vector <int> g[N];
struct Query
{
int k, id;
};
vector <Query> q[N];
void dfs1 (int x, int fa)
{
for (int i : g[x]) if (i ^ fa)
{
s[i] = s[x] + 1;
dfs1(i, x);
if (d[i] > d[son[x]]) son[x] = i;
}
d[x] = d[son[x]] + 1;
}
void dfs2 (int x, int fa)
{
f[x][0] ^= w[x];
if (son[x])
{
f[son[x]] = f[x] + 1;
dfs2(son[x], x);
}
for (int i : g[x]) if (i ^ son[x] && i ^ fa)
{
f[i] = cur, cur += d[i];
dfs2(i, x);
for (int j = 1; j <= d[i]; j++)
f[x][j] ^= f[i][j - 1];
}
for (Query i : q[x])
ans[i.id] = i.k < 0 || i.k >= d[x] || (f[x][i.k] & -f[x][i.k]) == f[x][i.k];
}
int main ()
{
read(n, m);
for (int i = 2, a; i <= n; ++i)
read(a), g[a].push_back(i);
for (int i = 1; i <= n; ++i)
{
char c = getchar();
while (c < 'a' || c > 'z') c = getchar();
w[i] = 1 << c - 'a';
}
dfs1(1, 0);
for (int i = 1, t, k; i <= m; ++i)
{
read(t), read(k);
q[t].push_back((Query){k - s[t] - 1, i});
}
f[1] = cur, cur += d[1];
dfs2(1, 0);
for (int i = 1; i <= m; ++i)
puts(ans[i] ? "Yes" : "No");
return 0;
}