Killing Dragons
Time limit2sMemory limit256 MB
Compute the smallest number of warriors who move along roads and cut heads faster than dragons regrow them to kill all dragons.
Problem
Dragon country has cities and roads. The cities are numbered through , and each road joins two different cities. Every road can be walked in both directions.
dragons live in the country. Dragon lives in city , starts with heads, and grows new heads every minute while it is alive. A dragon with at least one head is alive, and a dragon whose heads are all cut off dies. A dead dragon never grows a head again.
You hire warriors to wipe the dragons out. You pick the starting city of every warrior, and from minute 1 on, each minute runs in this order.
- Every warrior does one of three things. Move to a city joined to the current city by a road. Pick one living dragon in the current city and cut off one of its heads. Do nothing.
- Once all of that minute's actions are done, every dragon that still has at least one head grows more heads.
Several dragons may live in one city, and several warriors may cut heads off the same dragon in the same minute. Warriors travel only along roads, so they cannot move between cities that no chain of roads connects.
You choose the starting city and every action of every warrior. Find the smallest number of warriors that kills all of the dragons within a finite number of minutes.
Input
The input holds several test cases.
The first line of each test case has three integers , , (, , ). Each of the next lines holds one road as two integers , (), a road between city and city . The same pair of cities may be given more than once. Each of the next lines holds one dragon, and line has three integers , , (, , ).
The line after the last test case is 0 0 0, and that line is not processed.
Output
For each test case, print on one line the smallest number of warriors that kills all of the dragons.