#P8127. [BalticOI 2021] The Xana coup (Day2)

[BalticOI 2021] The Xana coup (Day2)

Description

You are given a tree with NN vertices. Vertex ii has a value aia_i, where ai{0,1}a_i \in \{0,1\}.

You may perform toggle operations:

  • Toggling vertex ii will flip the values of vertex ii and all vertices directly connected to it.

Here, “directly connected” means that there is exactly one edge between the two vertices.

Find the minimum number of toggle operations needed to make the values of all vertices equal to 00.

Input Format

The first line contains an integer NN, the number of vertices in the tree.

The next N1N-1 lines each contain two integers, describing an edge of the tree.

The (N+1)(N+1)-th line contains NN integers aia_i, representing the value of vertex ii.

Output Format

If a solution exists, output one line with one integer, the answer.

If there is no solution, output impossible.

5
1 2
1 3
2 4
2 5
0 1 0 1 1
4
5
1 2
2 3
3 4
4 5
0 1 1 1 1
impossible

Hint

Explanation for Sample 1

ai=0a_i=0 is colored black, and ai=1a_i=1 is colored white.

By toggling vertices 4,5,3,14,5,3,1, you can make the values of all vertices equal to 00.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (5 pts): N20N \le 20.
  • Subtask 2 (15 pts): N40N \le 40.
  • Subtask 3 (10 pts): If vertices uu and vv satisfy uv=1|u-v|=1, then there is an edge between them.
  • Subtask 4 (40 pts): Each vertex is connected to at most 33 vertices.
  • Subtask 5 (30 pts): No special restrictions.

For 100%100\% of the testdata, 3N1053 \le N \le 10^5.

Note

Translated from BalticOI 2021 Day2 C The Xana coup.

Translated by ChatGPT 5