Under construction

Still being built. Things may change

Mexor tree

Time limit2sMemory limit1024 MB

Summary
Given a tree with path-XOR updates, find for each vertex the smallest non-negative integer missing from the values on its path from S after all updates.
Level

Medium7 of 10

Topics
Tree, Segment tree, DFS, Bit manipulation
Solved
No attempts yet

Problem

A tree with NN vertices is given. Vertex ii has value aia_i.

There are MM queries in total.

  • xx yy zz: Replace each value on the simple path between vertices xx and yy with the result of XOR-ing it with zz.

After performing all MM queries, define a new sequence bib_i as follows.

  • bib_i is the smallest non-negative integer that does not appear among the values on the simple path between vertex SS and vertex ii. (1≤i≤N)(1 \leq i \leq N)

Find all bib_i.

Input

The first line contains integers NN and SS separated by a space. (1≤N≤300 000,1≤S≤N)(1 \leq N \leq 300\,000, 1 \leq S \leq N)

The second line contains the sequence a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N separated by spaces. (0≤ai≤N)(0 \leq a_i \leq N)

From the third line, the next N−1N-1 lines each contain two integers u,vu, v separated by a space, meaning there is an edge between vertex uu and vertex vv. (1≤u,v≤N)(1 \leq u,v \leq N)

Line N+2N+2 contains the number of queries MM. (1≤M≤300 000)(1 \leq M \leq 300\,000)

From line N+3N+3, the next MM lines each contain three integers x,y,zx, y, z separated by spaces. (1≤x,y,z≤N)(1 \leq x,y,z \leq N)

Output

Print the sequence b1,b2,⋯ ,bNb_1, b_2, \cdots, b_N separated by spaces on the first line.

Examples1

  1. Example 1

    Input
    3 1
    0 3 2
    1 2
    1 3
    2
    1 2 2
    1 3 2
    
    Expected output
    1 2 1