Unusual Entertainment (Codeforces 1899G)
Tags: Others, shortest paths

A tree is a connected graph without cycles.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[5, 1, 3, 2, 4]$$$ is a permutation, but $$$[2, 1, 1]$$$ is not a permutation (as $$$1$$$ appears twice in the array) and $$$[1, 3, 2, 5]$$$ is also not a permutation (as $$$n = 4$$$, but $$$5$$$ is present in the array).After a failed shoot in the BrMeast video, Alex fell into depression. Even his birthday did not make him happy. However, after receiving a gift from Timofey, Alex's mood suddenly improved. Now he spent days playing with the gifted constructor. Recently, he came up with an unusual entertainment.Alex builds a tree from his constructor, consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with the root at vertex $$$1$$$. Then he writes down each integer from $$$1$$$ to $$$n$$$ in some order, obtaining a permutation $$$p$$$. After that, Alex comes up with $$$q$$$ triples of integers $$$l, r, x$$$. For each triple, he tries to determine if there is at least one descendant of vertex $$$x$$$ among the vertices $$$p_l, p_{l+1}, \ldots, p_r$$$.A vertex $$$u$$$ is a descendant of vertex $$$v$$$ if and only if $$$\mathrm{dist}(1, v) + \mathrm{dist}(v, u) = \mathrm{dist}(1, u)$$$, where $$$\mathrm{dist}(a, b)$$$ is the distance between vertices $$$a$$$ and $$$b$$$. In other words, vertex $$$v$$$ must be on the path from the root to vertex $$$u$$$.Alex told Zakhar about this entertainment. Now Alex tells his friend $$$q$$$ triples as described above, hoping that Zakhar can check for the presence of a descendant. Zakhar is very sleepy, so he turned to you for help. Help Zakhar answer all of Alex's questions and finally go to sleep.

Input: The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.The first line of each test case contains two integers $$$n, q$$$ ($$$1 \le n, q \le 10^5$$$) — the number of vertices in the tree and the number of questions, respectively.Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$), indicating that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$ (it is guaranteed that the resulting graph is a tree).The next line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$ (it is guaranteed that each integer from $$$1$$$ to $$$n$$$ appears exactly once).Then follow $$$q$$$ lines describing Alex's questions. The $$$i$$$-th line contains three integers $$$l, r, x$$$ ($$$1 \le l \le r \le n$$$, $$$1 \le x \le n$$$), as described in the statement.It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$.

Output: For each of Alex's questions, print "Yes" (without quotes) if the described descendant exists, otherwise print "No" (without quotes).You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).


Sample Inputs:
3
3 5
1 2
2 3
1 2 3
1 2 2
1 2 3
2 3 1
1 2 3
2 3 3
10 10
2 6
2 7
2 4
1 7
2 8
10 6
8 5
9 4
3 4
10 2 5 9 1 7 6 4 3 8
8 9 8
7 8 1
7 10 6
4 8 9
5 5 10
7 10 1
9 9 2
9 10 6
6 6 2
10 10 6
1 1
1
1 1 1

Sample Outputs:
YES
NO
YES
NO
YES

NO
YES
YES
YES
NO
YES
YES
NO
NO
NO

YES

Solution (Python):
from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

class BIT:

    def __init__(self, n):
        self.nums = [0] * (n + 1)

    def low_bit(self, x):
        return x & -x

    def update(self, idx, d):
        while idx < len(self.nums):
            self.nums[idx] += d
            idx += self.low_bit(idx)

    def sum(self, x):
        ans = 0
        while x:
            ans += self.nums[x]
            x -= self.low_bit(x)
        return ans

    def sum2(self, l, r):
        return self.sum(r) - self.sum(l - 1)

@bootstrap
def dfs(p, cur):
    for (l, r, idx) in child[cur]:
        ans[idx] -= bit.sum2(l, r)
    bit.update(pos[cur], 1)
    for i in tree[cur]:
        if i == p:
            continue
        yield dfs(cur, i)
    for (l, r, idx) in child[cur]:
        ans[idx] += bit.sum2(l, r)
    yield
t = int(input())
for _ in range(t):
    (n, q) = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        tree[u].append(v)
        tree[v].append(u)
    p = list(map(int, input().split()))
    pos = [-1] * (n + 1)
    for i in range(n):
        pos[p[i]] = i + 1
    child = [[] for _ in range(n + 1)]
    ans = [0] * q
    for i in range(q):
        (l, r, x) = map(int, input().split())
        child[x] += [(l, r, i)]
    bit = BIT(n)
    dfs(0, 1)
    for i in range(q):
        print('yes' if ans[i] else 'no')
    print()

Solution (Python):
from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

class BIT:

    def __init__(self, n):
        self.nums = [0] * (n + 1)

    def low_bit(self, x):
        return x & -x

    def update(self, idx, d):
        while idx < len(self.nums):
            self.nums[idx] += d
            idx += self.low_bit(idx)

    def sum(self, x):
        ans = 0
        while x:
            ans += self.nums[x]
            x -= self.low_bit(x)
        return ans

    def sum2(self, l, r):
        return self.sum(r) - self.sum(l - 1)
t = int(input())
for _ in range(t):
    (n, q) = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        tree[u].append(v)
        tree[v].append(u)
    p = list(map(int, input().split()))
    pos = [-1] * (n + 1)
    for i in range(n):
        pos[p[i]] = i + 1
    child = [[] for _ in range(n + 1)]
    ans = [0] * q
    for i in range(q):
        (l, r, x) = map(int, input().split())
        child[x] += [(l, r, i)]
    bit = BIT(n)

    @bootstrap
    def dfs(p, cur):
        for (l, r, idx) in child[cur]:
            ans[idx] -= bit.sum2(l, r)
        bit.update(pos[cur], 1)
        for i in tree[cur]:
            if i == p:
                continue
            yield dfs(cur, i)
        for (l, r, idx) in child[cur]:
            ans[idx] += bit.sum2(l, r)
        yield
    dfs(0, 1)
    for i in range(q):
        print('yes' if ans[i] else 'no')
    print()

Solution (Python):
from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

class BIT:

    def __init__(self, n):
        self.nums = [0] * (n + 1)

    def low_bit(self, x):
        return x & -x

    def update(self, idx, d):
        while idx < len(self.nums):
            self.nums[idx] += d
            idx += self.low_bit(idx)

    def sum(self, x):
        ans = 0
        while x:
            ans += self.nums[x]
            x -= self.low_bit(x)
        return ans

    def sum2(self, l, r):
        return self.sum(r) - self.sum(l - 1)
t = int(input())
for _ in range(t):
    (n, q) = map(int, input().split())
    tree = [[] for _ in range(n + 1)]
    for i in range(n - 1):
        (u, v) = map(int, input().split())
        tree[u].append(v)
        tree[v].append(u)
    p = list(map(int, input().split()))
    pos = [-1] * (n + 1)
    for i in range(n):
        pos[p[i]] = i + 1
    child = [[] for _ in range(n + 1)]
    ans = [0] * q
    for i in range(q):
        (l, r, x) = map(int, input().split())
        child[x] += [(l, r, i)]
    bit = BIT(n)

    @bootstrap
    def dfs(p, cur):
        for (l, r, idx) in child[cur]:
            ans[idx] -= bit.sum2(l, r)
        bit.update(pos[cur], 1)
        for i in tree[cur]:
            if i == p:
                continue
            yield dfs(cur, i)
        for (l, r, idx) in child[cur]:
            ans[idx] += bit.sum2(l, r)
        yield
    dfs(0, 1)
    for i in range(q):
        print('yes' if ans[i] else 'no')
    print()