#P8127. [BalticOI 2021] The Xana coup (Day2)
[BalticOI 2021] The Xana coup (Day2)
Description
You are given a tree with vertices. Vertex has a value , where .
You may perform toggle operations:
- Toggling vertex will flip the values of vertex 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 .
Input Format
The first line contains an integer , the number of vertices in the tree.
The next lines each contain two integers, describing an edge of the tree.
The -th line contains integers , representing the value of vertex .
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

is colored black, and is colored white.
By toggling vertices , you can make the values of all vertices equal to .
Constraints
This problem uses bundled testdata.
- Subtask 1 (5 pts): .
- Subtask 2 (15 pts): .
- Subtask 3 (10 pts): If vertices and satisfy , then there is an edge between them.
- Subtask 4 (40 pts): Each vertex is connected to at most vertices.
- Subtask 5 (30 pts): No special restrictions.
For of the testdata, .
Note
Translated from BalticOI 2021 Day2 C The Xana coup.
Translated by ChatGPT 5
京公网安备 11011102002149号