Chemical Reactions
InterviewTime limit1sMemory limit512 MB
Count unowned compounds that become producible by repeatedly firing reactions whose substrates are all available.
Problem
Bajtek has recently taken up chemistry and become fascinated by it, so he built his own laboratory. He already owns a set of chemical compounds along with the tools needed to run various reactions. Now he wants to grow his collection, and he is curious how many different compounds that he does not yet own he could produce from what he has.
Assume every compound Bajtek already owns is available in unlimited supply. A reaction can be carried out whenever Bajtek owns all of its substrates; carrying it out gives him all of its products, which he may then use in further reactions.
Write a program that reads the compounds Bajtek owns and the reactions he can perform, then determines how many compounds he does not yet own but is able to produce.
Input
The first line contains three integers , , and (, , ), separated by single spaces: the number of compounds known to Bajtek, the number of compounds he owns, and the number of reactions he can perform.
The second line contains distinct integers (), the numbers of the compounds Bajtek owns.
Each of the next lines describes one reaction. A description starts with an integer (), the number of substrates, followed by the substrate compound numbers. It then continues with an integer (), the number of products, followed by the product compound numbers. Every compound number is between and . The substrate numbers of a reaction are pairwise distinct, and so are its product numbers, but a compound may appear as both a substrate and a product of the same reaction (acting as a catalyst). All numbers describing one reaction are separated by single spaces.
Output
Output a single integer: the number of compounds Bajtek does not yet own but can produce from the compounds he owns by carrying out the reactions.
Hint
In the sample the four reactions are:
Bajtek starts with compounds and . Reaction 1 lets him obtain and , and then reaction 3 produces . He cannot obtain compounds , , or . Hence he can make new compounds (, , and ).