#P14655. 同归月

同归月

Problem Description

Little L gave you a tree with n1n-1 edges. The ii-th edge is (ui,vi)(u_i, v_i). Some edges already have fixed directions. Little L wants you to determine the direction of every remaining edge so that the following conditions are satisfied:

  • For each node ii, let kik_i be the number of incoming edges of node ii. Then Wi,ki=1W_{i,k_i}=1, where Wi,jW_{i,j} is an input array with values in {0,1}\{0,1\}.
  • Let the 0101 sequence sis_i be defined as follows: if the ii-th edge is directed from uiu_i to viv_i, then si=0s_i=0; otherwise si=1s_i=1. Based on this, output the lexicographically smallest sequence sis_i.

If no solution exists, output 1-1. Otherwise, output one line representing the lexicographically smallest sis_i.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains an integer nn.

Then follow n1n-1 lines, each containing two integers ui,viu_i, v_i.

Then follow nn lines, each being a 0101 string of length di+1d_i+1, representing Wi,0Wi,diW_{i,0}\to W_{i,d_i}, where did_i is the degree of node ii.

Then follows one string of length n1n-1 consisting of 0, 1, and ?. Here 0 and 1 mean the corresponding edge direction is already fixed, and ? means it is not fixed.

Output Format

If there is no solution, output -1.

Otherwise, output one line: a 0101 string of length n1n-1 representing ss.

3
4
2 1
4 2
1 3
010
100
11
10
???
5
5 3
1 5
4 3
2 1
010
10
010
10
001
?0?0
3
3 1
3 2
01
10
010
??
-1
1000
01

Hint

For 20%20\% of the testdata, T20T\leq 20 and n16n\leq 16.

For 40%40\% of the testdata, n100\sum n\leq 100.

For 60%60\% of the testdata, n2000\sum n\leq 2000.

For the other 20%20\% of the testdata, the input string contains only ?.

For 100%100\% of the testdata, 2n5×1052\leq \sum n\leq 5\times 10^5 and n2n\geq 2.

Translated by ChatGPT 5