loj#P3563. 「BalticOI 2021 Day2」The Xana coup
「BalticOI 2021 Day2」The Xana coup
Description
Having to visit the museum day after day as an art critic—and not being allowed to touch the ex-hibits! turned out to be too much for you. Therefore, you decided to give your career as an art thief another try. However, after the total disaster of your debut, you are determined to do something about the surveillance cameras this time.
Accordingly, you have used your IT skills to hack into the camera control system. Unfortunately, the cameras themselves are part of the recent installation artwork Xanadu. This leads to a rather strange behaviour: There are cameras (numbered ) distributed over the museum, some of which might already be turned off for artistic reasons. The cameras are connected by wires in such a way that any two of them are connected to each other either directly or indirectly. The camera control system offers a button for each individual camera. However, pressing such a button does not only toggle this single camera, but also all cameras directly connected to it.
You are worried that your hacking effort might get noticed if you interact with the camera control system too much. Write a program that calculates the minimal number of button presses necessary to switch off all cameras.
Input
The first line contains an integer , the number of cameras in the museum.
Each of the following lines contains two integers and . This means that cameras and are directly connected by a wire.
The last line contains integers. The -th of these integers is if camera is turned on at the beginning, and if camera is turned off.
Output
Your program should output a single line. This line should consist of an integer, the minimum number of button presses required to turn off all cameras, or the string impossible
if it is not possible to turn off all cameras.
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
Constraints
It always holds that .
- Subtask 1(5 points). .
- Subtask 2(15 points). .
- Subtask 3(10 points). Cameras and are directly connected if and only if .
- Subtask 4(40 points). Every camera is directly connected to at most other cameras.
- Subtask 5(30 points). No further constraints.