Diamonds
Time limit1sMemory limit128 MB
Arthas opens boxes outward from his starting keys to collect every diamond with the fewest openings.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Intervals, Graph
- Solved
- No attempts yet
Problem
King Terenas ordered his minister to build a game that would sharpen the mind of his son, prince Arthas. The minister came up with a game that uses boxes labeled through . Every box has its own lock, and a lock opens only with the key cut for that box.
Before the game starts, the minister copies the key of box and puts one copy in box and one copy in box , skipping whichever of the two does not exist. He then places one diamond in each of distinct boxes. Finally he locks every box and hands Arthas the keys to of them.
Arthas can open any box whose key he holds, and when he opens a box he takes everything inside it, both the keys and the diamond. The keys he takes let him open more boxes. Find the smallest number of boxes Arthas has to open to collect every diamond.
Input
The input contains several test cases. The first line of each test case holds the number of boxes (), the number of keys handed to Arthas (), and the number of diamonds (), separated by spaces. The second line lists the labels of the boxes whose keys Arthas received. The third line lists the labels of the boxes that contain a diamond, and this line is empty when is .
The last line of the input is 0 0 0.
Output
For each test case, print on one line the minimum number of boxes Arthas has to open to collect all diamonds.