Treeversion

All submissions for this problem are available.
### Read problem statements in [Hindi](http://www.codechef.com/download/translated/LTIME76/hindi/TREEVERS.pdf), [Bengali](http://www.codechef.com/download/translated/LTIME76/bengali/TREEVERS.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/LTIME76/mandarin/TREEVERS.pdf), [Russian](http://www.codechef.com/download/translated/LTIME76/russian/TREEVERS.pdf), and [Vietnamese](http://www.codechef.com/download/translated/LTIME76/vietnamese/TREEVERS.pdf) as well. You are given a rooted tree with $N$ nodes (numbered $1$ through $N$); the root is node $1$. For each valid $i$, node $i$ has weight $w_i$, which is either $0$ or $1$. We want to traverse the tree using depth first search. The order in which the nodes are visited is not uniquely defined, since we may visit the children of each node in an arbitrary order. Formally, the pseudocode of DFStraversal is: ``` function DFS(node n): node n is visited for each node s (s is a son of n) in some order: call DFS(s) return call DFS(root) ``` For each possible DFStraversal of the tree, consider the sequence of weights of nodes in the order in which they are visited; each node is visited exactly once, so this sequence has length $N$. Calculate the number of inversions for each such sequence. The minimum of these numbers is the *treeversion* of our tree. Find the treeversion of the given tree. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains a single integer $N$.  The second line contains $N$ spaceseparated integers $w_1, w_2, \ldots, w_N$.  Each of the following $N1$ lines contains two spaceseparated integers $x$ and $y$ denoting that nodes $x$ and $y$ are connected by an edge. ### Output For each test case, print a single line containing one integer — the treeversion of the given tree. ### Constraints  $1 \le T \le 1,000$  $1 \le N \le 10^5$  $0 \le w_i \le 1$ for each valid $i$  $1 \le x, y \le N$  the graph on the input is a tree  the sum of $N$ over all test cases does not exceed $10^6$ ### Subtasks **Subtask #1 (50 points)**:  $1 \le N \le 1,000$  the sum of $N$ over all test cases does not exceed $10,000$ **Subtask #2 (50 points)**: original constraints ### Example Input ``` 1 3 1 0 1 1 2 1 3 ``` ### Example Output ``` 1 ```Author:  erfaniaa 
Editorial  https://discuss.codechef.com/problems/TREEVERS 
Tags  deadwing97, erfaniaa, ltime76 
Date Added:  26092019 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions