Ellie has a tree with $$$n$$$ nodes. Each node $$$i$$$ has a binary value $$$a_i$$$ which is either $$$0$$$ or $$$1$$$.
In a post-apocalyptic digital world, where every bit counts for survival, Ellie must eliminate all traces of digital presence by performing the following operation any number of times:
Ellie wants to convert all values in the tree to $$$0$$$ to escape detection. Your task is to determine if this is possible, and if so, find the minimum number of operations required.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 3 \cdot 10^5$$$) — the number of nodes in the tree.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$a_i \in \{0, 1\}$$$) — the initial values of the nodes.
The next $$$n - 1$$$ lines describe the edges of the tree. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$), denoting an undirected edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the given input forms a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, if it is impossible to convert all values to $$$0$$$, output $$$-1$$$. Otherwise, output the minimum number of operations required.
280 0 1 1 0 1 0 02 87 51 85 16 73 54 360 0 0 1 0 13 64 22 56 11 2
-1 3
In the first test case, it is impossible to convert all values to $$$0$$$, so the output is $$$-1$$$.
In the second test case, one possible sequence of operations with the minimum number of operations is:
Figure: tree from the second test case.