Watchdogs
Time limit5sMemory limit1024 MB
Given a tree and mouse paths, find the fewest vertices that include at least one middle vertex of every path.
- Level
Medium6 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
In Kocourkov, a small town surrounded by hills and woods, mice have become a problem. The town was peaceful for years, known for its cozy pubs and friendly people. Then the mice arrived. They are smart and quick, and they seem to be everywhere at once. They have taken over the town's favorite pubs, running through the rafters, nibbling on crumbs, and causing a lot of trouble.
Kocourkov has places and exactly roads, and you can reach any place from any other place by using the roads. Each road connects a pair of places.
The town council met in the largest pub, U Nacpané Myši. They noticed that the mice run across the city in a specific pattern. Each mouse has exactly two lairs, and it runs only between them. Each lair is in one of the places. The council also observed that a mouse is vulnerable about halfway along its usual path. Formally, if a mouse has lairs at places and , then the mouse is vulnerable at place if and only if and is on the path from to . Here is the number of roads on the path from to . A mouse may be vulnerable at several places, and it may also be vulnerable at its own lair.
The town will place watchcats around the city. A watchcat can get rid of a mouse if the watchcat sits at a place where the mouse is vulnerable. One watchcat can get rid of any number of mice. Watchcats are lazy. Each one sits at its chosen place and never moves.
Watchcats are expensive, so the town council wants to buy as few as possible. Help them with this task.
Input
The first line contains two integers and (, ), the number of places and the number of mice.
Each of the next lines contains two integers and (), meaning that places and are connected by a direct road.
Each of the next lines contains two integers and (), meaning that a mouse has lairs at places and . It is guaranteed that .
Output
Print a single integer, the minimum number of watchcats needed so that every mouse is eventually caught.