You are given a tree with vertices numbered from to . Vertex has a positive integer written on it.
If two vertices have equal values, that is, , you may choose the path between and in the tree. The chosen path contains both endpoints and every vertex between them.
Only the endpoint values must be equal; the values at internal vertices may be anything. For example, if the tree is and its values are , you may choose the path containing all three vertices.
You may also choose the same vertex as both endpoints. Such a path consists of just that vertex.
Choose paths satisfying both of the following conditions.
You do not have to use every edge. Paths follow the original edges of the tree; choosing a path does not add a new edge.
Find the minimum number of paths needed. A valid choice always exists by choosing a separate single-vertex path for each vertex.
The input is given from Standard Input in the following format:
For each test case, print the minimum number of paths on its own line.
In the first case, the single path covers every vertex. Both endpoint values are ; the internal value does not matter. In the second case, choose path and the single-vertex path . They are vertex-disjoint and each has equal endpoint values. One path cannot cover all vertices, so the answer is . In the third case, the only vertex forms one path.
The first line contains the number of test cases . Each case is given in the following format:
The first line of each case contains the number of vertices . The second line contains the values .
The -th of the next lines describes an edge between vertices and . When , no edge lines are given.
| 3 | 14 | , (). |
| 4 | 18 | In each test case, . The sum of over all test cases is at most . |
| 5 | 22 | In each test case, . The sum of over all test cases is at most . |
| 6 | 31 | No additional constraints. |
Choose path and the single-vertex paths and to cover the tree with three paths. Path also has equal endpoint values, but it shares vertex with , so those two paths cannot be chosen together. Covering every vertex with at most two paths is impossible.
No two distinct vertices have equal values, so every vertex must be its own single-vertex path.