#P4657. [CEOI 2017] Chase
[CEOI 2017] Chase
Description
In front of the fugitive there is a maze. The maze consists of rooms and bidirectional corridors. Each corridor connects two different rooms, and all rooms are reachable from each other through the corridors. In other words, it is a tree.
The fugitive will choose a room to enter the maze, walk through some corridors, and then leave the maze, but he will never walk through the same corridor twice.
In room , there are iron balls. Whenever someone passes through this room, they will be blocked by the iron balls. The fugitive has magnets. When he arrives at a room, he may choose to drop one magnet (or not). This will pull all iron balls from every room adjacent to this room into this room. The process is as follows:
- The fugitive enters the room.
- The fugitive drops a magnet.
- The fugitive leaves the room.
- The iron balls are pulled into this room.
Note that the fugitive is only blocked by the iron balls originally in this room, and will not be blocked by the iron balls pulled into this room.
After the fugitive leaves the maze, the pursuer will go through the maze along the path taken by the fugitive, and he will encounter all iron balls on this path.
Please help the fugitive choose a path to maximize: (number of iron balls encountered by the pursuer) minus (number of iron balls encountered by the fugitive).
Input Format
The first line contains two space-separated integers and .
The second line contains space-separated integers representing .
Then follow lines. Each line contains two space-separated integers and , indicating that there is a corridor connecting room and room .
Output Format
Output one integer, representing the maximum possible value of (number of iron balls encountered by the pursuer) minus (number of iron balls encountered by the fugitive) in the optimal case.
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
36
Hint
Sample Explanation
One optimal plan is as follows:
- Enter the maze from room and drop the first magnet. He encounters iron balls. At this time, room will have iron balls, while rooms , , , and will have no iron balls.
- Walk to room , drop the second magnet, and leave the maze. He encounters iron balls. At this time, room will have iron balls, while rooms , , , and will have no iron balls.
In this process, the fugitive encounters iron balls, while the pursuer encounters iron balls.
Constraints
For of the testdata, 。
Translated by ChatGPT 5
京公网安备 11011102002149号