#P14655. 同归月
同归月
Problem Description
Little L gave you a tree with edges. The -th edge is . 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 , let be the number of incoming edges of node . Then , where is an input array with values in .
- Let the sequence be defined as follows: if the -th edge is directed from to , then ; otherwise . Based on this, output the lexicographically smallest sequence .
If no solution exists, output . Otherwise, output one line representing the lexicographically smallest .
Input Format
The first line contains an integer , the number of test cases.
For each test case, the first line contains an integer .
Then follow lines, each containing two integers .
Then follow lines, each being a string of length , representing , where is the degree of node .
Then follows one string of length 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 string of length representing .
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 of the testdata, and .
For of the testdata, .
For of the testdata, .
For the other of the testdata, the input string contains only ?.
For of the testdata, and .
Translated by ChatGPT 5