CF BUDDY
← Problems·

1902F · Trees and XOR Queries Again

2400 · data structures, dfs and similar, divide and conquer

Problem: You are given a tree consisting of nn vertices. There is an integer written on each vertex; the ii-th vertex has integer aia_i written on it.

You have to process qq queries. The ii-th query consists of three integers xix_i, yiy_i and kik_i. For this query, you have to answer if it is possible to choose a set of vertices v1,v2,,vmv_1, v_2, \dots, v_m (possibly empty) such that:

  • every vertex vjv_j is on the simple path between xix_i and yiy_i (endpoints can be used as well);
  • av1av2avm=kia_{v_1} \oplus a_{v_2} \oplus \dots \oplus a_{v_m} = k_i, where \oplus denotes the bitwise XOR operator.

Input Format: The first line contains one integer nn (2n21052 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (0ai22010 \le a_i \le 2^{20} - 1).

Then n1n-1 lines follow. Each of them contains two integers uu and vv (1u,vn1 \le u, v \le n; uvu \ne v) denoting an edge of the tree.

The next line contains one integer qq (1q21051 \le q \le 2 \cdot 10^5) — the number of queries.

Then qq lines follow. The ii-th of them contains three integers xix_i, yiy_i and kik_i (1xi,yin1 \le x_i, y_i \le n; 0ki22010 \le k_i \le 2^{20} - 1).

Output Format: For each query, print YES if it is possible to form a set of vertices meeting the constraints. Otherwise, print NO.

You can print each letter in any case.

Sample Cases

Case 1

Input

4
0 1 2 10
2 1
3 2
4 2
8
3 3 0
3 4 1
3 4 7
1 3 1
1 3 2
1 3 10
1 4 10
1 4 11

Output

YES
YES
NO
YES
YES
NO
YES
YES

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback