| TeamsCode 2026 Spring Contest |
|---|
| Finished |
There is a rooted tree with $$$n$$$ vertices rooted at vertex $$$1$$$. Each vertex holds a binary value $$$v_i$$$ ($$$0$$$ or $$$1$$$). In one move, you choose a vertex $$$v$$$ and flip the value of every vertex on the path from $$$v$$$ to the root (inclusive). Find the minimum number of moves to make all vertices equal to $$$1$$$.
The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$), denoting the number of nodes in the tree.
The next line contains $$$n$$$ integers $$$v_i$$$ ($$$v_i \in \{0, 1\}$$$), with the $$$i$$$-th integer denoting the value of node $$$i$$$.
The next $$$n-1$$$ lines contain $$$2$$$ integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting an edge connecting $$$a_i$$$ and $$$b_i$$$ in the tree.
—
Tests in subtasks are numbered from $$$1−25$$$ with samples skipped. Each test is worth $$$\frac{100}{25}=4$$$ points.
Tests $$$1-3$$$ satisfy $$$n \leq 20$$$.
Tests $$$4-10$$$ satisfy that the tree is a path. That is, there is an edge from node $$$i$$$ to node $$$i+1$$$ for all $$$1\le i\le n-1$$$.
Tests $$$11-25$$$ satisfy no additional constraints.
Output one integer, the minimum number of moves to end with all vertices equal to $$$1$$$.
41 0 0 11 21 32 4
2
—
Problem Idea: jay_jayjay
Problem Preparation: theyashb
Occurrences: Novice C
| Name |
|---|


