Tree Requests
Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n - 1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi, the parent index is always less than the index of the vertex (i.e., pi < i).The depth of the vertex is the number of nodes on the path from the root to v along the edges. In particular, the depth of the root is equal to 1.We say that vertex u is in the subtree of vertex v, if we can get from u to v, moving from the vertex to the parent. In particular, vertex v is in its subtree.Roma gives you m queries, the i-th of which consists of two numbers vi, hi. Let's consider the vertices in the subtree vi located at depth hi. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used.
String s is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome.Clarification for the sample test.In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z".In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them.In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome.In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome.In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".
The first line contains two integers n, m (1 ≤ n, m ≤ 500 000) — the number of nodes in the tree and queries, respectively.The following line contains n - 1 integers p2, p3, ..., pn — the parents of vertices from the second to the n-th (1 ≤ pi < i).The next line contains n lowercase English letters, the i-th of these letters is written on vertex i.Next m lines describe the queries, the i-th line contains two numbers vi, hi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in the i-th query.
Print m lines. In the i-th line print "Yes" (without the quotes), if in the i-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).
import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def make_graph(n, m):
    (x, s) = ([0] * (2 * m), [0] * (n + 3))
    for i in range(m):
        (u, v) = (p[i + 2], i + 2)
        s[u + 2] += 1
        (x[2 * i], x[2 * i + 1]) = (u, v)
    for i in range(3, n + 3):
        s[i] += s[i - 1]
    G = [0] * m
    for i in range(0, 2 * m, 2):
        j = x[i] + 1
        G[s[j]] = x[i ^ 1]
        s[j] += 1
    return (G, s)

def euler_tour():
    s = []
    s.append(1)
    now = list(s0)
    t = 1
    (t1, t2) = ([-1] * (n + 1), [-1] * (n + 1))
    dist = [inf] * (n + 1)
    dist[1] = 1
    v = []
    while s:
        i = s[-1]
        if t1[i] == -1:
            v.append(i)
            t1[i] = t
            t += 1
        if now[i] == s0[i + 1]:
            t2[i] = t
            t += 1
            s.pop()
        else:
            s.append(G[now[i]])
            dist[s[-1]] = dist[i] + 1
            now[i] += 1
    return (t1, t2, dist, v)
(n, m) = map(int, input().split())
p = [0, 0] + list(map(int, input().split()))
s = list(input().rstrip())
s = [0] + [i - 97 for i in s]
(G, s0) = make_graph(n, n - 1)
inf = pow(10, 9) + 1
(t1, t2, dist, v) = euler_tour()
l0 = max(dist[1:]) + 5
x = [[] for _ in range(l0)]
y = [[0] for _ in range(l0)]
pow2 = [1]
for _ in range(26):
    pow2.append(2 * pow2[-1])
for i in v:
    j = dist[i]
    x[j].append(t1[i])
    y[j].append(y[j][-1] ^ pow2[s[i]])
z = set(pow2 + [0])
ans = []
for _ in range(m):
    (v, h) = map(int, input().split())
    if dist[v] == h or h >= l0 or (not x[h]):
        ans0 = 'Yes'
    else:
        xh = x[h]
        l = bisect.bisect_left(xh, t1[v])
        r = bisect.bisect_right(xh, t2[v])
        ans0 = 'Yes' if y[h][l] ^ y[h][r] in z else 'No'
    ans.append(ans0)
sys.stdout.write('\n'.join(ans))
import bisect
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 4096

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
sys.stdin = IOWrapper(sys.stdin)
sys.stdout = IOWrapper(sys.stdout)
input = lambda : sys.stdin.readline().rstrip('\r\n')
I = lambda : int(input())
MI = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
N = 500005
(n, m) = MI()
tree = [[] for i in range(N)]
for (y, x) in enumerate(LI(), start=1):
    tree[x - 1].append(y)
s = input()
A = [0] * 26
for i in range(26):
    A[i] = 1 << i
in_seq = [0] * N
out_seq = [0] * N
ts = 0
hh_ts = [[0] for _ in range(N)]
hh_xor = [[0] for _ in range(N)]
stk = [[0, 0, 0]]
while stk:
    (x, h, flag) = stk.pop()
    if flag == 0:
        ts += 1
        in_seq[x] = ts
        hh_ts[h].append(ts)
        hh_xor[h].append(hh_xor[h][-1] ^ A[ord(s[x]) - 97])
        stk.append([x, h + 1, 1])
        for y in tree[x]:
            stk.append([y, h + 1, 0])
    else:
        ts += 1
        out_seq[x] = ts
for i in range(m):
    (v, h) = MI()
    v -= 1
    h -= 1
    l = bisect.bisect_left(hh_ts[h], in_seq[v])
    r = bisect.bisect_left(hh_ts[h], out_seq[v])
    if l >= r:
        print('Yes')
        continue
    l = max(l - 1, 0)
    r -= 1
    t = hh_xor[h][l] ^ hh_xor[h][r]
    if t & t - 1 == 0:
        print('Yes')
    else:
        print('No')
import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def make_graph(n, m):
    (x, s) = ([0] * (2 * m), [0] * (n + 3))
    for i in range(m):
        (u, v) = (p[i + 2], i + 2)
        s[u + 2] += 1
        (x[2 * i], x[2 * i + 1]) = (u, v)
    for i in range(3, n + 3):
        s[i] += s[i - 1]
    G = [0] * m
    for i in range(0, 2 * m, 2):
        j = x[i] + 1
        G[s[j]] = x[i ^ 1]
        s[j] += 1
    return (G, s)

def euler_tour():
    s = []
    s.append(1)
    now = list(s0)
    t = 1
    (t1, t2) = ([-1] * (n + 1), [-1] * (n + 1))
    dist = [inf] * (n + 1)
    dist[1] = 1
    v = []
    while s:
        i = s[-1]
        if t1[i] == -1:
            v.append(i)
            t1[i] = t
            t += 1
        if now[i] == s0[i + 1]:
            t2[i] = t
            t += 1
            s.pop()
        else:
            s.append(G[now[i]])
            dist[s[-1]] = dist[i] + 1
            now[i] += 1
    return (t1, t2, dist, v)
(n, m) = map(int, input().split())
p = [0, 0] + list(map(int, input().split()))
s = list(input().rstrip())
s = [0] + [i - 97 for i in s]
(G, s0) = make_graph(n, n - 1)
inf = pow(10, 9) + 1
(t1, t2, dist, v) = euler_tour()
l0 = max(dist[1:]) + 5
x = [[] for _ in range(l0)]
y = [[0] for _ in range(l0)]
pow2 = [1]
for _ in range(26):
    pow2.append(2 * pow2[-1])
for i in v:
    j = dist[i]
    x[j].append(t1[i])
    y[j].append(y[j][-1] ^ pow2[s[i]])
z = set(pow2 + [0])
ans = []
for _ in range(m):
    (v, h) = map(int, input().split())
    if dist[v] == h or h >= l0 or (not x[h]):
        ans0 = 'Yes'
    else:
        xh = x[h]
        l = bisect.bisect_left(xh, t1[v])
        r = bisect.bisect_right(xh, t2[v])
        ans0 = 'Yes' if y[h][l] ^ y[h][r] in z else 'No'
    ans.append(ans0)
sys.stdout.write('\n'.join(ans))